Cant get check on directory existing to work

Jw4096

New Member
Joined
Jul 17, 2015
Messages
11
I have tried a few different ways of checking now and none have worked so far. Here is the most recent version which did not work:

Code:
Public Function MakeFolder(Directory As String, FolderName As String) 
    
    Dim TestDir As String
    
    TestDir = Dir(Directory + "/" + FolderName)
    
    If TestDir = "" Then
        MsgBox "File doesn't exist"
        MkDir (Directory + "/" + FolderName)
    Else
        MsgBox "File exist"
    End If
    
End Function

This will make the folder in the right directory but if I run the function again with the same parameters and the folder already existing it will tell me the folder does not exist and then I will get #VALUE instead of 0 in the cell which looks bad.

TestDir is always blank even when the directory does exist.

What am I doing wrong?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi,

Try the following;

Code:
Public Function MakeFolder(Directory As String, FolderName As String)
    
    Dim TestDir As String
    
    TestDir = Directory + "\" + FolderName
    
    If Dir(TestDir, vbDirectory) = "" Then
        MsgBox "File doesn't exist"
        MkDir (Directory + "\" + FolderName)
    Else
        MsgBox "File exist"
    End If
    
End Function
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,930
Members
449,479
Latest member
nana abanyin

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