VBA to create folder

data808

Active Member
Joined
Dec 3, 2010
Messages
358
Office Version
  1. 2019
Platform
  1. Windows
I have some VBA that looks for a specific folder by the current year in this format YYYY and then names the excel file and saves it in that current year YYYY folder. Here is the code:

VBA Code:
ActiveWorkbook.SaveAs Filename:="K:\BACKEND\RECORDS\CLERK\" & Format(Now(), "YYYY") & "\" _
    & "ABC_" & Format(Now(), "MM-DD-YYYY") & ".xlsm"

Usually the folder is always there but when the new year comes for 2025, instead of manually creating a folder every year, I was wondering if there was a way that if the YYYY folder does NOT exist, can it auto create one to save the file in it?

How would I write the VBA to do that if possible? Thanks for the help.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
This thread covers pretty much the same exact request.

 
Upvote 0
Thanks for the help. I was able to figure it out using that thread you suggested. Thanks again.
 
Upvote 0
In case anyone comes to this thread, here is what worked for me:

VBA Code:
'this section is to create a current year folder if it doesn't exist
'be sure to change the source directory for the (respective clerk)
Dim sourceDir As String
Dim year As Integer

'get year
year = Trim(Str(Format(Date, "yyyy")))

'source directory (respective clerk)
sourceDir = "C:\Users\Username\Desktop"

'check if current year folder exists, if no current folder then create one
folder_exists = Dir(sourceDir & "\" & year, vbDirectory)
If folder_exists = "" Then
    MkDir sourceDir & "\" & year
    folder_exists = Dir(sourceDir & "\" & year, vbDirectory)
    MsgBox "Current Year Folder Did Not Exist." & vbNewLine & _
    "Folder Created And File Will Now Save In It."
End If

ActiveWorkbook.SaveAs Filename:="C:\Users\Username\Desktop\" _
    & Format(Now(), "YYYY") & "\" _
    & "CLK_" & Format(Now(), "MM-DD-YYYY") & ".xlsm"

Basically, it looks for the current year folder on the desktop (change directory to your fitting). If folder does not exist it will prompt user to let them know a current year folder has been created and it will now save into that folder. If folder already exists then it will just save the file to that current year folder. Hope this helps out someone.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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