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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
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
 

joefrench

Active Member
Joined
Oct 4, 2006
Messages
357
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
 

joefrench

Active Member
Joined
Oct 4, 2006
Messages
357
I found my error. It was in one of the cell references.

Thank you for your help.
 

Forum statistics

Threads
1,136,575
Messages
5,676,644
Members
419,636
Latest member
phownz

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
Top