How to bypass MkDir when that Dir exists already

  • Thread starter Jan Nademlejnsky
  • Start date
J

Jan Nademlejnsky

I have macro which creates Subdirectories. I need to bypass MkDir command if
that folder already exists. How do you do that?

Thanks for your help.

Jan
 
J

Jan Nademlejnsky

I found it:

Sub DirectoryExists()
FPath = Range("B2")
If Dir(FPath) = "" Then '.....If
Dir("C:\Users\Owner\Documents\ExcelF\") = "" Then
MsgBox "It does not exists"
Else
MsgBox "It is already there"
End If
End Sub
 
H

Helmut Meukel

Jan Nademlejnsky said:
I have macro which creates Subdirectories. I need to bypass MkDir command if
that folder already exists. How do you do that?

Thanks for your help.

Jan


As others already sugested, check if the Dir exists.

Or try to create the Dir and simply ignore the error:

On Error Resume Next 'instruct VBA to ignore any error
MkDir "MyNewSubdir"
On Error GoTo 0 'no error handling
or
On Error GoTo MyErrorHandler 'reenable your previous
'error handling

Helmut.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top