How to check if a folder/directory exist using VBA

W

wellie

Can someone please tell me how can I check if a
directory/folder already exist using VBA within Excel ?

I tried the followings and it did not work

if Len(Dir("c:\mypath\myfolder")) = 0 then
mkdir("c:\mypath\myfolder\newfolder")
else
msgbox "Folder 'myfolder' already exist."

endif

Regards
 
T

Tom Ogilvy

You can skip the test

On Error Resume Next
mkdir("c:\mypath\myfolder\newfolder")
On Error goto 0

this assumes mypath and mypath\myfolder exist.

If not, you can use the same principle


On Error Resume Next
mkdir("C:\mypath")
mkdir("C:\mypath\myfolder")
mkdir("c:\mypath\myfolder\newfolder")
On Error goto 0
 

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