VBA to Save Worksheet With Current Date (Opening New Folder For Month / Year)

Small Paul

Board Regular
Joined
Jun 28, 2018
Messages
118
Hi All
I have a number of worksheets produced each day which need saving.
The worksheet name is formatted as "yyyy mm dd"

I need it saved in the relevant month's folder and, if not available, set that folder up. The month folder is set as, for example, "08 August" and "09 September". This way, they sit in order within the year folder.

Ideally, the VBA would also set up a year folder when required.

Looking through other posts, the best I can find is:
Code:
    Dim fPath As String    MkDir "Z:\Orders\2-Open Trades" & Format(Date, "yyyy") & "" & Format(Date, "mm-yy")
    fPath = "Z:\Orders\2-Open Trades" & Format(Date, "yyyy") & "" & Format(Date, "mm-yy")

which doesn't actually save the file at all!

The directory for the main file is:

Z:\Orders\2-Open Trades\yyyy\[08 August] see above\filename.xls

I can then adjust (primary folders) accordingly.

Any assistance would be appreciated.

Many thanks
Small Paul.
 
ah because its not the default drive you need to set it (chdrive)

try:
Code:
Sub dfhh()
Dim strDir As String, DflDir As String, YrDir As String, WB1 As Workbook
    DflDir = "Z:\Orders\2-Open Trades\"
    YrDir = (DflDir & Format(Date, "yyyy"))
    strDir = (YrDir & "\" & Format(Date, "mm") & " " & Format(Date, "mmmm"))
    Set WB1 = ThisWorkbook
    
    If Dir(DflDir, vbDirectory) = "" Then MkDir DflDir
    If Dir(YrDir, vbDirectory) = "" Then MkDir YrDir
    If Dir(strDir, vbDirectory) = "" Then MkDir strDir
    ChDrive strDir
    ChDir strDir
    Filename = Format(Now, "yyyy-mm-dd")
    WB1.SaveCopyAs Filename:=Filename & ".xlsm"
    
End Sub
 
Last edited:
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
It makes sense to my tiny brain but still does not save!
I have put as:
Code:
    With Range("H2:K500")        .HorizontalAlignment = xlCenter
    End With
    Range("A1").Select
Dim strDir As String, DflDir As String, YrDir As String, WB1 As Workbook
    DflDir = "Z:\Orders\2-Open Trades\"
    YrDir = (DflDir & Format(Date, "yyyy"))
    strDir = (YrDir & "\" & Format(Date, "mm") & " " & Format(Date, "mmmm"))
    Set WB1 = ThisWorkbook
    
    If Dir(DflDir, vbDirectory) = "" Then MkDir DflDir
    If Dir(YrDir, vbDirectory) = "" Then MkDir YrDir
    If Dir(strDir, vbDirectory) = "" Then MkDir strDir
    ChDrive strDir
    ChDir strDir
    Filename = Format(Now, "yyyy-mm-dd")
    WB1.SaveCopyAs Filename:=Filename & ".xlsm"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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