automatically save a workbook

newshound12

Active Member
Joined
Feb 19, 2003
Messages
339
When closing a workbook, automatically save it as an .xlsm workbook with next Sunday’s date (MM-DD-YY) in a folder created also with next Sunday’s date.
Also save an .xlsx copy of the workbook with the name of the day of the week (Monday thru Saturday, Sunday’s save will be the actual date) in the same folder.
The .xlsm can be overwritten after a message box asks “Do you want to overwrite existing file or rename it as a copy”.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
When closing a workbook, automatically save it as an .xlsm workbook with next Sunday’s date (MM-DD-YY) in a folder created also with next Sunday’s date.

Run the macro below and look in VBA Immediate window - display that window with {CTRL} G when in VBA editor
VBA Code:
Sub IsThisCorrect()

    Dim DateString As String, CreateThisFolder, SaveFileWithThisName, FullSaveName
    Const DefaultPath = "C:\Folder\Subfolder"
    Const DefaultName = "Name of Workbook "

    DateString = Format(Date + 6 - WorksheetFunction.Weekday(Date, 3), "MM DD YY")
    CreateThisFolder = DefaultPath & "\" & DateString
    SaveFileWithThisName = DefaultName & DateString & ".xlsm"
    FullSaveName = CreateThisFolder & "\" & SaveFileWithThisName
   
    Debug.Print "Folder:", CreateThisFolder
    Debug.Print "File:", SaveFileWithThisName
    Debug.Print "Path:", FullSaveName

End Sub

Are the created strings returning the correct workbook name, subfolder and save to path ?
 
Upvote 0
Thanks for your reply.
I see the code in the immediate window.
I can make adjustments to suit my needs.
How do I get the code to work with the workbook?
 
Upvote 0
Simply amend the default path string and default name string to those you require
WARNING - The default path must already exist, otherwise the code will fail :eek:

VBA Code:
Sub SaveSunday()
    Dim DateString As String, CreateThisFolder, SaveFileWithThisName, FullSaveName
    Const DefaultPath = "C:\Folder\Subfolder"
    Const DefaultName = "Name of Workbook "

    DateString = Format(Date + 6 - WorksheetFunction.Weekday(Date, 3), "MM DD YY")
    CreateThisFolder = DefaultPath & "\" & DateString
    SaveFileWithThisName = DefaultName & DateString & ".xlsm"
    FullSaveName = CreateThisFolder & "\" & SaveFileWithThisName 
'create folder
    On Error Resume Next
        MkDir CreateThisFolder
    On Error GoTo 0
'save under new name
    ThisWorkbook.SaveAs FullSaveName
End Sub

Above is the code previously supplied but updated to create a folder and save the file
If, for any reason, the code does not work when you test it, please post your COMPLETE amended code
- click on the </> icon above post window
- paste the code into the window which appears
- set Language to VBA code
- click on Continue.
 
Upvote 0
Also save an .xlsx copy of the workbook with the name of the day of the week (Monday thru Saturday, Sunday’s save will be the actual date) in the same folder.

You want 6 copies of the workbook created with 6 different names - what is the required name string ?
my guess is
default name(as above) + SundayDateString(as above) + Monday + extension .xlsx
ie something like this ...
Name of Workbook 12 15 19 Monday.xlsx
 
Upvote 0
To test the code beow
- place code in ThisWorkbook Module
- close the workbook with X in top right corner
- message appears
- answer yes to run the macro,


The VBA goes in ThisWorkbook code module - does not work anywhere else

Workbook_BeforeClose.jpg


Simply call the other macro from Workbook_BeforeClose like this

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If MsgBox("Create weekly files?", vbYesNo, "Weekly Job") = vbYes Then
        Call SaveSunday
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,466
Members
449,086
Latest member
kwindels

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