VBA Create folder and sub folder

jdluke6545

Board Regular
Joined
Jul 30, 2021
Messages
53
Office Version
  1. 2019
Platform
  1. Windows
I am using this code, which is working perfectly:
VBA Code:
Sub MakeFolderAndSave()
    Dim Path As String
    Dim fldr As String
    Dim filename As String
    Path = "C:\Users\JD\Documents\Completed Time Sheets\" & Format(Range("AD5"), "mm-yyyy")
    fldr = Dir(Path, vbDirectory)
        If fldr = "" Then MkDir Path
    filename = "LE " & Range("AK10").Value & " " & Format(Range("AD5").Value, "mm-dd-yyyy") & " - " & Range("A4").Value & " - " & Range("A6").Value
    ThisWorkbook.SaveAs filename:=Path & "\" & filename
End Sub
Which looks to see if the folder "mm-yyyy" exists in "C:\Users\JD\Documents\Completed Time Sheets\" and if not it creates the folder.
Cell AD5 is a date

Now, what I am wanting to do is go a step further and create a folder for the year "yyyy" and then the sub folder "mm-yyyy" for keeping things better organized.
So, I thought I could just edit the PATH line to:
VBA Code:
Path = "C:\Users\JD\Documents\Completed Time Sheets\" & Format(Range("AD5"), "yyyy") \ Format(Range("AD5"), "mm-yyyy")
But this results in an error: Run-time error '13': Type mismatch
I want the code to create the folder named for the year "yyyy" if it is not already created, and then create the sub-folder "mm-yyyy" if it is not already created also.
If the folder for year "yyyy" is already created then just create the sub-folder "mm-yyyy".
If both folders, "yyyy" and "mm-yyyy" are already there, then just go to FILENAME line and save the file in the corresponding folder
What am I forgetting here? Or what am i doing wrong?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
The backslash character "\" when used without quotes is an operator that is meant for integer division, not as path seperator as one would expect. When dividing a number by another, all its operands will be converted to integers, implicitly.

For example,
VBA Code:
Dim result As Variant
result = "123.456" \ 10
The string "123.456" will be implicitly converted to 123 of data type Integer, before divided by 10. If during this conversion (or more technically, "type coercion") VBA fails to convert from one type to another, you get that type mismatch error. For example the following line:
VBA Code:
result = "hello" \ 10
will not run fine at runtime.

In your specific case, the backslash should be used as a string, not operator. It should be surrounded with double quotes and concatenated with other strings with ampersand("&") character.

Hope you don't think I'm talking too much.
 
Upvote 0
I actually tried that before, putting the backslash in quotes...."\" but still have an error...
 
Upvote 0
When i put the backslash in double quotes, "\" I get the error:
Compile error: Expected: end of statement
I get this right after typing the code..
 
Upvote 0
I have tried:
VBA Code:
Path = "C:\Users\JD\Documents\Completed Time Sheets\" & Format(Range("AD5"), "yyyy") & "\" & Format(Range("AD5"), "mm-yyyy")
and when the code is run I get an error that says "path not found"
 
Upvote 0
Hello,

Did you make sure the year folder and month folder are created in two steps ? (1 step/folder) You can not create a folder and a sub folder in one step.

As indicated by Jack, your instruction should be like this
VBA Code:
Path = "C:\Users\JD\Documents\Completed Time Sheets\" & Format(Range("AD5"), "yyyy") & "\" & Format(Range("AD5"), "mm-yyyy")
 
Upvote 0
Hello,

Did you make sure the year folder and month folder are created in two steps ? (1 step/folder) You can not create a folder and a sub folder in one step.

As indicated by Jack, your instruction should be like this
VBA Code:
Path = "C:\Users\JD\Documents\Completed Time Sheets\" & Format(Range("AD5"), "yyyy") & "\" & Format(Range("AD5"), "mm-yyyy")
Oh ok,,, so i need a line of code to first create the year folder, and then if the year folder exist move to the next line and create the mm-yyyy folder?
Let me see if i can do that...
Thanks!!
 
Upvote 0
Exactly, 1 folder at a time. Reusing the code you posted, i think the proposition below should work.
VBA Code:
Sub MakeFolderAndSave()
    Dim Path As String
    Dim fldr As String
    Dim filename As String
    ' making the year folder
    Path = "C:\Users\JD\Documents\Completed Time Sheets\" & Format(Range("AD5"), "yyyy")
    fldr = Dir(Path, vbDirectory)
        If fldr = "" Then MkDir Path
    ' making the month sub-folder
    Path = Path & "\" & Format(Range("AD5"), "mm-yyyy")
    fldr = Dir(Path, vbDirectory)
        If fldr = "" Then MkDir Path
   
    filename = "LE " & Range("AK10").Value & " " & Format(Range("AD5").Value, "mm-dd-yyyy") & " - " & Range("A4").Value & " - " & Range("A6").Value
    ThisWorkbook.SaveAs filename:=Path & "\" & filename
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,353
Messages
6,184,457
Members
453,233
Latest member
bgmb

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