Make folder not quite working correctly

erutherford

Active Member
Joined
Dec 19, 2016
Messages
449
If the folder doesn't exit, the code functions fine creating a folder in the correct path. If the folder already exist, the If statement doesn't do its job. Its has to be something simple

Code:
Sub CreateFolder()

'Variable declaration
    Dim sFolderPath As String

    'Define Folder Path
    sFolderPath = "C:\MyCar_Photos"

    'Check Specified Folder exists or not
    If Dir(sFolderPath) <> "" Then
        'If folder is available
        MsgBox "Folder exists!", vbInformation, "MyCar Photos"
        Exit Sub
    End If

    MkDir sFolderPath

End Sub
[/code}
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Since you're searching for a folder/directory instead of a file, you need to set the attribute for Dir to vbDirectory...

VBA Code:
If Dir(sFolderPath, vbDirectory) <> "" Then

Hope this helps!
 
Upvote 0
Solution
yep something simple. A little side note. Probably most of us beginners search all over the net looking for the answers to our excel problems. Some of these sites have a pretty big presence and provide samples of their work, but I am wondering why they don't "check" their work before posting, Frustrating!
The one good thing is that the answer always seems to come from here, MrExcel and the MVPs! Thanks for your help Domenic!
 
Upvote 0
Yeah, after so many years, MrExcel is still my favourite site. :)

And, you're very welcome, I'm glad I could help.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,178
Members
449,071
Latest member
cdnMech

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