VBA Save Backup Copy of Workbook before changing original

skydiver

New Member
Joined
Aug 20, 2008
Messages
23
I am trying to create a macro that takes an open workbook, saves a copy of the file as "BACKUP-" in the same directory of the original then goes back to the original workbook and does the modifications I require. I have the backup file being created just fine only when I start to perform the code to modify the original workbook sheet, it is still working on the backup copy of the workbook. How to I prevent the SaveAs file method from taking focus and preventing the changes from being made to the original file?

Code:
Sub Week()
    Dim Sourcewb  As Workbook
    Dim Sourcewbname As String
    Dim Backupwb As Workbook
    Dim Workingpath As String
    Dim BackupfName As String
    Dim TempBackupfName As String
    
    Set Backupwb = ActiveWorkbook
    Set Sourcewb = ActiveWorkbook
    
    Workingpath = Destinationwb.Path & "\"
    TempBackupfName = Destinationwb.Name
    BackupfName = Workingpath & "BACKUP-" & TempBackupfName
    With Destinationwb
        .SaveAs Filename:=BackupfName, _
        FileFormat:=52, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
    End With
  
    Sourcewb.Sheets("Inventory").Select
    ActiveWindow.SmallScroll ToRight:=4
    
    Range("n16:n276").Select
    Selection.Copy
    Range("o16").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveWindow.ScrollColumn = 9
    ActiveWindow.ScrollColumn = 8
    ActiveWindow.ScrollColumn = 7
    ActiveWindow.ScrollColumn = 2
    Range("g16:g276").Select
    Selection.ClearContents
    
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Maybe something like:
Code:
Windows("yourOriginalWorkbookName").Activate
After the SaveAs
 
Upvote 0
I am trying to create a macro that takes an open workbook, saves a copy of the file as "BACKUP-" in the same directory of the original then goes back to the original workbook and does the modifications I require. I have the backup file being created just fine only when I start to perform the code to modify the original workbook sheet, it is still working on the backup copy of the workbook. How to I prevent the SaveAs file method from taking focus and preventing the changes from being made to the original file?

Code:
Sub Week()
    Dim Sourcewb  As Workbook
    Dim Sourcewbname As String
    Dim Backupwb As Workbook
    Dim Workingpath As String
    Dim BackupfName As String
    Dim TempBackupfName As String
    
    Set Backupwb = ActiveWorkbook
    Set Sourcewb = ActiveWorkbook
    
    Workingpath = Destinationwb.Path & "\"
    TempBackupfName = Destinationwb.Name
    BackupfName = Workingpath & "BACKUP-" & TempBackupfName
    With Destinationwb
        .SaveAs Filename:=BackupfName, _
        FileFormat:=52, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
    End With
  
    Sourcewb.Sheets("Inventory").Select
    ActiveWindow.SmallScroll ToRight:=4
    
    Range("n16:n276").Select
    Selection.Copy
    Range("o16").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveWindow.ScrollColumn = 9
    ActiveWindow.ScrollColumn = 8
    ActiveWindow.ScrollColumn = 7
    ActiveWindow.ScrollColumn = 2
    Range("g16:g276").Select
    Selection.ClearContents
    
End Sub

Does adding this change help?

Code:
    With Destinationwb
        .SaveAs Filename:=BackupfName, _
        FileFormat:=52, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
    End With
    
    ActiveWorkbook.SaveAs Filename:=TempBackupfName, _
        FileFormat:=52, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
 
Upvote 0
I have temporarily forced the save into the same folder without user intervention with this code:

Code:
 Dim Destinationwb As Workbook
    Dim Workingpath As String
    Dim BackupfName As String
    Dim TempBackupfName As String
   
    Set Destinationwb = ActiveWorkbook
        
    Workingpath = Destinationwb.Path & "\"
    TempBackupfName = Destinationwb.Name
    BackupfName = Workingpath & "BACKUP ResetWeek-" & TempBackupfName
    Destinationwb.SaveCopyAs Filename:=BackupfName
            
    Sheets("Inventory").Select
    ActiveWindow.SmallScroll ToRight:=4
    
    Range("n16:n276").Select
    Selection.Copy
    Range("o16").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveWindow.ScrollColumn = 9
    ActiveWindow.ScrollColumn = 8
    ActiveWindow.ScrollColumn = 7
    ActiveWindow.ScrollColumn = 2
    Range("g16:g276").Select
    Selection.ClearContents
    
End Sub

The two limitations with my workaround code are
- If I have the original file version <12 xls files with macros I want to save as a 2007 version with macros enabled (xlsm)
- I want to offer the option to change the save to folder by opening the save as dialog in the current working directoy an pre-populating the filename


I will try the two suggestions to see if they work for me and report the results back.
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,519
Members
452,921
Latest member
BBQKING

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