Forcing folder creation

thekamel

New Member
Joined
Feb 11, 2015
Messages
16
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hello everyone,

I am using a script that will save a file to a specific folder based of a couple of cells. If the folder doesn't exist, the command fails. I cannot figure out how to get it to create the folder if it isn't there. Here is what I am using now:

Sub File_Save()

Dim Path1 As String
Dim Path2 As String
Dim myfilename As String
Dim fpathname As String

Path1 = Range("K278")
Path2 = Range("K279")
myfilename = Range("K280")
fpathname = Path1 & "\" & Path2 & myfilename & ".xlsm"

MsgBox "You are trying to save the file to:" & vbCrLf & fpathname
ActiveWorkbook.SaveAs Filename:=fpathname, FileFormat:=xlOpenXMLWorkbookMacroEnabled

End Sub

Any help is greatly appreciated!

Thank you,
Kamel
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Untested, but try this:

VBA Code:
Sub File_Save()
'
    Dim Path1       As String, Path2        As String
    Dim fpathname   As String, myfilename   As String
'
    Path1 = Range("K278")
    Path2 = Range("K279")
    myfilename = Range("K280")
'
    If Len(Dir(Path1)) = 0 Then MkDir Path1
    If Len(Dir(Path2)) = 0 Then MkDir Path2
'
    fpathname = Path1 & "\" & Path2 & myfilename & ".xlsm"
'
    MsgBox "You are trying to save the file to:" & vbCrLf & fpathname
'
    ActiveWorkbook.SaveAs Filename:=fpathname, FileFormat:=xlOpenXMLWorkbookMacroEnabled
End Sub
 
Upvote 0
Hello,​
or just using MakeSureDirectoryPathExists like in this thread :​
 
Upvote 0
A VBA demonstration to paste to the top of the module (for Excel versions prior to 2010 version remove PtrSafe statement) :​
VBA Code:
Private Declare PtrSafe Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal DirPath$) As Boolean

Sub Demo1()
    Dim F$
        F = Join$([TRANSPOSE(K278:K280)], "\")
        If MakeSureDirectoryPathExists(F) Then ActiveWorkbook.SaveAs F, 52 Else Beep
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,093
Latest member
ripvw

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