Macro - Save specific open wrksheet with a varying file name

wrmcmahan12

New Member
Joined
May 13, 2015
Messages
12
Hello Everyone,

Hopefully this is an easy fix. I have a large file that I backup every 30 minutes due to it corrupting frequently. I have created a macro to save a new version of the file with a variable name (based on date and time) as follow:
"YYYY-MM-DD-HH-MM_FileName.xlsm". It works out great when its the only file in use, but I have realized if I have it open with other files, which ever file that is active at that 30 minute mark is the one to get saved. Is there a way to tell the macro to activate the specific workbook I need (which name is variable) and save it? Currently I tried referencing a cell that has the file name in it, but still no luck. The only other idea I had was to save a backup, and then resave the file within the same job to a common name that is more or less static. Any thoughts? Current code below: (Private subs in workbook module, others in regular module)

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime dTime, "Save_Backup", , False
End Sub

Private Sub Workbook_Open()

dTime = Now + TimeValue("00:00:05")
Application.OnTime dTime, "Save_Backup"
End Sub

Public dTime As Date
Sub Save_Backup()

dTime = Now + TimeValue("00:30:00")
Application.OnTime dTime, "Save_Backup"
ActiveWorkbook.SaveAs Filename:="File path etc" & Format(Now, "yyyy-mm-dd-hh-mm") & "_File_Name.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False

End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Re: Need help with macro - Save specific open wrksheet with a varying file name

Code:
[COLOR=#000080][I]Instead of[/I][/COLOR]
ActiveWorkbook.SaveAs Filename:="File path etc...
[I][COLOR=#000080]Try[/COLOR][/I][COLOR=#ff0000]
[/COLOR]ThisWorkbook.SaveAs Filename:="File path etc...

The macro is in the file being saved, so you can refer with ThisWorkbook
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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