Excel VBA - Creating Directories

  • Thread starter Thread starter th1chsn
  • Start date Start date
T

th1chsn

Hello, I was hoping some of you on this forum will be able to help m
out with a VBA problem I seem to be having. I want to create
directory but before I create the directory I want to check to see i
it exists first.

If it exists then I want to skip creating the directory. If it doesn'
then I want to create the directory.

It sounds simple but I am having nothing but trouble trying to get i
to work.

TIA
 
Easier is to just do this:

On Error Resume Next
mkdir "C:\MyFolders\MyFolders1"
On Error goto 0

This assumes that C:\MyFolders exists
 
You could try this:

Public Sub CreateDirectory()

Dim strPath as string

strPath = "Your directory path goes here"

'Check to see if it exists with Dir function
If Dir(strPath) = "" Then
MkDir(strPath)
End if

End Sub
 
Hi,

On Error Resume Next ' Handle error
MkDir "C:\Temp"
On Error GoTo 0 ' Cancel error handling

The above will create the folder if it doesn't exist and will skip due to an
error if the folder exists.
regards,
Don
 

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

Back
Top