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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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