Before Save - Append Date

akaUriel

New Member
Joined
Jul 30, 2011
Messages
2
What I am trying to do is make it so the workbook will save and automatically append the date to the end of the filename, before the extension.

Code:
Dim dt As String, wbNam As String

wbNam = Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1)
dt = Format(CStr(Now), "mmddyy")
ActiveWorkbook.SaveAs Filename:=wbNam & " " & dt

This is the code I use to save the file, however I can't get it to run off the BeforeSave event.

Any suggestions?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_BeforeSave(<SPAN style="color:#00007F">ByVal</SPAN> SaveAsUI <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>, Cancel <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>)<br>    <SPAN style="color:#00007F">Dim</SPAN> dt <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, wbNam As <SPAN style="color:#00007F">String</SPAN><br><br>    wbNam = Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1)<br>    dt = Format(Date, "mmddyy")<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN><br>    ActiveWorkbook.SaveAs Filename:=wbNam & " " & dt<br>    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br>    Cancel = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Hello AlphaFrog,

Although it's good practice to not have . in the name of a workbook, I would use InStrRev instead of InStr.

More important, if you save the file twice (and more), all dates will be continuously added to the filename...

Therefore:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim dt As String, wbNam As String
    wbNam = Left(ThisWorkbook.Name, InStrRev(ThisWorkbook.Name, ".") - 8)
    dt = Format(Date, "mmddyy")
    On Error Resume Next
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:=wbNam & " " & dt
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    Cancel = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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