Backup Save in Addition to Regular Save

Vonsteiner

New Member
Joined
Apr 14, 2014
Messages
45
Office Version
  1. 365
Platform
  1. Windows
Greetings Everyone,

I hope everyone is well. I have userform code in place to save a file based on a cell value and to also save a backup file with the date added. Both of these work for the most part. Here is the code as I have it right now:

Code:
Dim wb As Workbook
Dim wbname As String

Set wb = ActiveWorkbook

wbname = ThisWorkbook.Path & "\" & _
    ThisWorkbook.Worksheets("Case Information").Range("A1").Value & " MTO "
    
wb.SaveAs wbname

Call BackupWorkbook

Backup Code
Code:
Dim wb2 As Workbook
Dim wbname2 As String


Set wb2 = ActiveWorkbook


wbname2 = ThisWorkbook.Path & "\Backups\" & _
    ThisWorkbook.Worksheets("Case Information").Range("A1").Value & " MTO " & _
    Format(Now(), "dd-mm-yyyy")
    
wb2.SaveAs wbname2

With the code in this order is saves both of the files in the correct place, but the backup file now has become the active workbook. If I reverse the code and have Excel save the backup first it saves both files in the Backups folder, but it does make my non-backup the active file. Is there a way to have Excel save both files correctly, but then have the non-backup file be the active workbook? Also, I have the code save the workbook after each userform using the wb.save command. Right now it would just save the non-backup. How would I include the backup being saved as well.

Michael
 

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).
Hi,
Use SaveCopyAs to make backups of your workbook
[FONT=&quot]This method saves a copy of your workbook to a file but doesn't modify the open workbook in memory.

Untested but you could modify your code as follows

Rich (BB code):
[/FONT]
Rich (BB code):
Dim wb As Workbook
Dim wbname As String


Set wb = ActiveWorkbook


wbname = ThisWorkbook.Path & "\" & _
    ThisWorkbook.Worksheets("Case Information").Range("A1").Value & " MTO "
    
wb.SaveAs wbname

'pass workbook object & file path to backup code
Call BackupWorkbook(wb, wbname)
[FONT=&quot]



'make backup copy

Rich (BB code):
[/FONT]
Rich (BB code):
Sub BackupWorkbook(ByVal wb As Object, ByVal BackupFilePath As String)
    wb.SaveCopyAs BackupFilePath & " " & Format(Now(), "dd-mm-yyyy")
End Sub
[FONT=&quot]



Dave[/FONT]
 
Upvote 0
Solution
The backup wasn't saving in the correct folder or as an Excel file. So I tweaked my original code and it works perfectly now. The 'SaveCopyAs' was the exact code I needed, Thank you Dave. I went with this to save the original and a backup:

Code:
Dim wb As Workbook
Dim wb2 As Workbook
Dim wbname As String
Dim wbname2 As String


Set wb = ActiveWorkbook
Set wb2 = ActiveWorkbook

'save file
wbname = ThisWorkbook.Path & "\" & _
    ThisWorkbook.Worksheets("Case Information").Range("A1").Value & " MTO" & ".xlsm"
    
wb.SaveAs wbname
    
'save a backup


wbname2 = ThisWorkbook.Path & "\Backups\" & _
   ThisWorkbook.Worksheets("Case Information").Range("A1").Value & " MTO " & _
    Format(Now(), "dd-mm-yyyy") & ".xlsm"


wb2.SaveCopyAs wbname2




Michael
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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