Backup creation upon open

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,390
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I want to create a backup of a file once I open it.

  • The backup needs to be done the first time the file is opened each week, but only after a Wednesday, so Wednesday, Thursday or Friday.
  • I only need it once per week and for the backup, just make a copy of the file.
  • The location of the backup files need to be stored in a backup folder that is located in the current directory of the spreadsheet. It then needs to be separated by folders of year and month.
  • The file name for the backup file needs to be "the name of the current file - the date the backup is run", for instance, "Sample_file - dd.mm.yy"
Thanks
 
FYI - My One Drive file locations can also be referenced as local file locations. If I open a file on my One Drive and print the
VBA Code:
ThisWorkbook.Path
it shows as
VBA Code:
"C:\Users\Me\OneDrive\Documents"
. How are you accessing One Drive?
 
Upvote 0

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.
I can't post the rest due to privacy reasons.

I have found this code that I am trying to use.

VBA Code:
Sub Save_Spreadsheet()
    Dim CurrentDate As String
    Dim FilePath As String
    Dim FileName As String
   
    CurrentDate = Format(Date, "yyyy_mm_dd")
   
    ' change the file directory and file name as needed.
    FilePath = "C:\Users\....."
    FileName = "MyWorkbook_"

    ActiveWorkbook.SaveAs FilePath & FileName & CurrentDate & ".xlsx"    
    MsgBox("File create succesfully")
   

End Sub

How do I get the current date to appear with the time as well?
 
Upvote 0
I wonder if swapping out all of the ThisWorkbook with ActiveWorkbook would work?
 
Upvote 0
VBA Code:
Sub Save_Spreadsheet()
    Dim CurrentDate As String
    Dim FilePath As String
    Dim FileName As String
   
    CurrentDate = Format(Date, "yyyy_mm_dd")
   
    ' change the file directory and file name as needed.
    FilePath = "C:\Users\....."
    FileName = "MyWorkbook_"

    ActiveWorkbook.SaveAs FilePath & FileName & CurrentDate & ".xlsx"   , xlOpenXMLWorkbook
    MsgBox("File create succesfully")
   

End Sub
 
Upvote 0
This is my code:

VBA Code:
Sub Save_Spreadsheet()
    Dim CurrentDate As String
    Dim FilePath As String
    Dim FileName As String
    
    CurrentDate = Format(Date, "yyyy_mm_dd")
    
    ' change the file directory and file name as needed.
    FilePath = "C:\Users\...."
    FileName = "MyWorkbook_"
    
    ActiveWorkbook.SaveAs FilePath & FileName & CurrentDate & ".xlsm"

End Sub

There is a location after C:\Users\ but I have had to remove it due to privacy.

What is the code to get a spreadsheet saved in "DD.MM.YY - HH.MM"?
 
Upvote 0
I tried to put it after current date but it gave me another error. I have attached it.

VBA Code:
Sub Save_Spreadsheet()
    Dim CurrentDate As String
    Dim FilePath As String
    Dim FileName As String
    
    CurrentDate = Date & "-" & Hour(Now()) & ":" & Minute(Now())
    'Format(Date, "yyyy_mm_dd")
    
    ' change the file directory and file name as needed.
    FilePath = "C:\Users\SpiritAbility\OneDrive - SpiritAbility\Reception Folder\Qualifications\Backups\"
    FileName = "MyWorkbook_"
    
    ActiveWorkbook.SaveAs FilePath & FileName & CurrentDate & ".xlsm"

End Sub
 

Attachments

  • error.png
    error.png
    8.1 KB · Views: 2
Upvote 0
That's because you can't have a colon in a file name.
VBA Code:
CurDateTime = Date & "-" & Hour(Now()) & "-" & Minute(Now())
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
Members
452,902
Latest member
Knuddeluff

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