VBA Backup Code for Excel that also uses Date Time

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
516
Office Version
  1. 365
Platform
  1. Windows
Hi All,
does anybody have a vba routine that will add Date & Time to the back up file name, however, I would like something a little more than

VBA Code:
ActiveWorkbook.SaveAs ("C:\MyDrive " & Format(Now(), "DD-MMM-YYYY hh mm AMPM") & ".xlsm")

I find the SAVE AS routine very annoying because of the change of active file when the SAVE AS is performed.

Ideally, I would like to:
Keep the same file name
Have the back up copy file name be Filename + Date & Time - so it is unique
But NOT change the active file.

Any ideas.

thanks
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try using SaveCopyAs method

Rich (BB code):
ActiveWorkbook.SaveCopyAs ("C:\MyDrive " & Format(Now(), "DD-MMM-YYYY hh mm AMPM") & ".xlsm")


Dave
 
Upvote 0
HI Dave,

Thanks for that suggestion.

I just tried changing to saving as .xls which is backward compatible to 2003.

Unfortunately, it still changed the active workbook.
 
Upvote 0
You can only use SaveCopyAs to save a copy of a file of same file format. SaveCopyAs method should not modify the open workbook in memory.

If your file is xlsm format there is no parameter in the method that allows you to change it to say xls format. To do this, you would need to write a custom SaveAs function.


Dave
 
Upvote 0
you could try
Rich (BB code):
Sub Auto_Save()
    Dim saveDate As Date
    Dim saveTime As Variant
    Dim formatTime As String
    Dim formatDate As String
    Dim backupFolder As String
    Dim FileExt As String
    Dim ThisFileName As String
          
    saveDate = Now
    FileExt = ".xlsx"
    
    formatDate = Format(saveDate, "MM-DD-YYYY hh.MM")
            
    Application.DisplayAlerts = False
    backupFolder = "C:\my drive\"
    
    ThisFileName = "name of file" & formatDate & FileExt
    
    ActiveWorkbook.SaveCopyAs FileName:=backupFolder & ThisFileName
    Application.DisplayAlerts = True
    
    MsgBox "Backup Successfully In The Path " & backupFolder
  
End If
End Sub
 
Upvote 0
you could try
Rich (BB code):
Sub Auto_Save()
    Dim saveDate As Date
    Dim saveTime As Variant
    Dim formatTime As String
    Dim formatDate As String
    Dim backupFolder As String
    Dim FileExt As String
    Dim ThisFileName As String
         
    saveDate = Now
    FileExt = ".xlsx"
   
    formatDate = Format(saveDate, "MM-DD-YYYY hh.MM")
           
    Application.DisplayAlerts = False
    backupFolder = "C:\my drive\"
   
    ThisFileName = "name of file" & formatDate & FileExt
   
    ActiveWorkbook.SaveCopyAs FileName:=backupFolder & ThisFileName
    Application.DisplayAlerts = True
   
    MsgBox "Backup Successfully In The Path " & backupFolder
 
End If
End Sub
Thanks rjmdc, I'll give this ago.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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