How do I check to see if a folder exists within a directory in excel VBA?

dan7055

Active Member
Joined
Jul 9, 2015
Messages
312
How can I check to see if a folder within a specific filepath already exists?

For example

If C:/desktop/example/folder/bob exists then

msgbox "This folder is already here"
exit sub

end if

What would the proper syntax for this be?

Thanks
Dan
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
You need to use the 2nd argument of Dir.
Code:
Sub CheckDirectory()
    If Dir("C:\desktop\example\folder\bob\", vbDirectory) <> "" Then
        MsgBox "This folder is already here"
    Else
        MsgBox "No, path does not exist."
    End If
End Sub
 
Upvote 0
You need to use the 2nd argument of Dir.
Code:
Sub CheckDirectory()
    If Dir("C:\desktop\example\folder\bob\", vbDirectory) <> "" Then
        MsgBox "This folder is already here"
    Else
        MsgBox "No, path does not exist."
    End If
End Sub

Not sure what you mean by "use the 2nd argument of Dir." Do you mean use the "Else" statement along with it?
 
Upvote 0
Dir takes 2 arguments, the second being optional.

To check for a directory you set the second argument to vbDirectory.
Rich (BB code):
Dir("C:\desktop\example\folder\bob\", vbDirectory)
 
Upvote 0

Forum statistics

Threads
1,214,963
Messages
6,122,484
Members
449,088
Latest member
Melvetica

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top