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”.
 
Q Is there a possibility that user would NOT want to overwrite the SUNDAY file but still want to create the Monday to Saturday files ?
If the answer to the above question is YES then we will need to modify the code slightly
- let me know after you have tested


To prevent files beingdisplayed out of sequence (Fri Mon Sat Thurs Tues Wed) in Windows Explorer ... each day is prefixed with a number
Array("1_Monday", "2_Tuesday", "3_Wednesday", "4_Thursday", "5_Friday", "6_Saturday")

If that is not what you want. ...replace with
Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")


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

    DateString = Format(Date + 6 - WorksheetFunction.Weekday(Date, 3), "MM DD YY")
    CreateThisFolder = DefaultPath & "\" & DateString
    On Error Resume Next
        MkDir CreateThisFolder
    On Error GoTo 0

'Sunday
    SaveFileWithThisName = DefaultName & DateString & ".xlsm"
    FullSaveName = CreateThisFolder & "\" & SaveFileWithThisName
    ThisWorkbook.SaveAs FullSaveName

'Monday to Saturday
    For Each NewDay In Array("1_Monday", "2_Tuesday", "3_Wednesday", "4_Thursday", "5_Friday", "6_Saturday")
        SaveFileWithThisName = DefaultName & DateString & " " & NewDay & ".xlsx"
        FullSaveName = CreateThisFolder & "\" & SaveFileWithThisName
        Application.DisplayAlerts = False
            ThisWorkbook.SaveAs FullSaveName, FileFormat:=xlOpenXMLWorkbook
        Application.DisplayAlerts = False
    Next NewDay
End Sub
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Now how to get daily saved files like "12 15 19 Monday.xlsx"
I was not sure what you wanted ..

So I went with Name of Workbook 12 15 19 Monday.xlsx
SaveFileWithThisName = DefaultName & DateString & " " & NewDay & ".xlsx"

But If you want 12 15 19 Monday.xlsx
replace with
SaveFileWithThisName = NewDay & ".xlsx"
 
Upvote 0

Forum statistics

Threads
1,216,086
Messages
6,128,736
Members
449,466
Latest member
Peter Juhnke

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