create a directory if not exist VBA

Memon

Board Regular
Joined
Jan 21, 2014
Messages
56
Good Morning/Evening everyone,
I have the following code which works fine IF the directory is present
but I want it to create the directory if not present

Code:
[SIZE=3]Dim thisfilepath As String
Dim fin As String
fin = Range("A21") & ".xlsx"
thisfilepath = Application.ActiveWorkbook.Path & "\orders-copy\" & fin


'---> code that check if the directory present and add it if not


Workbooks.Add
ActiveWorkbook.SaveAs Filename:=thisfilepath[/SIZE]
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Code:
vDir = "c:\myfolder\folder1\
IF NOT DirExists(vDir) then makedir vDir

'------------
Public Function DirExists(ByVal pvDir) As Boolean
'------------
  Dim fso
  Set fso = CreateObject("Scripting.FileSystemObject")
  DirExists = fso.FolderExists(pvDir)
  Set fso = Nothing
End Function


'------------
Public Sub MakeDir(ByVal pvDir)
'------------
  Dim fso
  Set fso = CreateObject("Scripting.FileSystemObject")
  If Not fso.FolderExists(pvDir) Then fso.CreateFolder pvDir     'MkDir pvDir
  Set fso = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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