Check for folder. If folder doesn't exist. create folder.

joefrench

Active Member
Joined
Oct 4, 2006
Messages
357
I want to run a macro prior to a save as macro that will check to see if a folder exists. If the folder does not exist, will then create the folder.

What is the best way (or any way) to go about doing this?
 

Excel Facts

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

You can use Dir to see if a directory exists, and then if needed MkDir to create it if it doesn't.
Code:
Dim strDir As String

strDir = "C:\My Directory\"

If Dir(strDir, vbDirectory) ="" Then
     MkDir strDir
Else
     Msgbox strDir & " already exists.
End If
 
Upvote 0
I have tried your code as you entered it and it worked great. However, after I entered my directory it says path not found. When I enter "debug mode" and hold my cursor over strDir, the tool tip shows the path just as I want to see it. I am wanting to write these files directly to the server....could this be the problem?

My code is as follows:
Code:
Sub ChkCreateFolder()
Dim strDir As String

strDir = "\\Cjdata1\design engineering common\Molds\" & Sheet2.Range("O1").value & "\" & Sheet1.Range("J4").Value & "\Router\"

If Dir(strDir, vbDirectory) = "" Then
     MkDir strDir
Else
     MsgBox strDir & " already exists. "
End If
End Sub

where Sheet2.Range("O1").value is an existing folder & Sheet1.Range("J4").Value is an existing folder[/code]

The tooltip ends up reading "Cjdata1\design engineering common\molds\2000-2099\2030\Router\" which is exactly what I want.

Thank you
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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