File backup on open + change save location

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Currently using the following code to backup a workbook when it is opened

VBA Code:
Dim sFileName As String
Dim sDateTime As String

    With ThisWorkbook
        sDateTime = " Backup Created (" & Format(Now, "dd-mm-yyyy hhnnss") & ").xlsm"
        sFileName = Application.WorksheetFunction.Substitute _
          (.FullName, ".xlsm", sDateTime)
        .SaveCopyAs sFileName
    End With

Currently, the backup is dropped in the same location as the main document. I've created a backup folder in the same location to which I wish to save backups - keep things a little tidier :)

However, I'm not sure what I should be changing with the above code. I've tried
VBA Code:
.SaveCopyAs "[pathway]" & sFileName
but no dice. I imagine it's something simple I'm missing, that's usually the way with VBA I find.

Any advice?

Thanks!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try
VBA Code:
    With ThisWorkbook
        sDateTime = " Backup Created (" & Format(Now, "dd-mm-yyyy hhnnss") & ").xlsm"
        sFileName = Application.WorksheetFunction.Substitute _
          (.Name, ".xlsm", sDateTime)
        .SaveCopyAs .Path & "\Backup\" & sFileName
    End With
 
Upvote 0
Try
VBA Code:
    With ThisWorkbook
        sDateTime = " Backup Created (" & Format(Now, "dd-mm-yyyy hhnnss") & ").xlsm"
        sFileName = Application.WorksheetFunction.Substitute _
          (.Name, ".xlsm", sDateTime)
        .SaveCopyAs .Path & "\Backup\" & sFileName
    End With
No dice with that one I'm afraid.

It seems to be getting thoroughly confused and gives a 1004 error - the message is "sorry, we couldn't find [filepath\Backups\\\filepath(yes, repeated for some reason)\filename]. Is it possible it was moved, renamed or deleted?

I've no idea why its repeating the filepath twice like that
 
Upvote 0
Did you use the code I provided, or did you change your your code?
 
Upvote 0
Where is the file located?
Is it on your hard drive, a server, or the cloud?
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,252
Members
449,305
Latest member
Dalyb2

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