Error in Excel macro for taking backup of excel sheet

manekankit

Board Regular
Joined
Feb 1, 2019
Messages
71
Office Version
  1. 365
Platform
  1. Windows
[FONT=&quot]I am looking to create backup of the current workbook.
The steps are as follows:
1. an excel file is opened
2. I am running be below code to create back up of the file
3. the file should create folder named "Export" if not already there
4. the file should create sub-folder named "Back up" in the "Export" folder if not already there
5. the file should create further sub-folder named which will be named in date format as "dd-mmm-yyyy" in the "Back up" sub folder, if not already there
6. then it should take back up of the file. the back up path should be the sub folder created at Sr.5 above (i.e. "dd-mmm-yyyy" and the file name should be time stamp (yyyy-mm-dd-hh-ss) and name of the opened file


I am using below code but it is returning error. Can you please help with this? Thanks.


Sub backupfile()
Dim ruta As String, ruta2 As String, ruta3 As String, ruta4 As String, nFile As String
nFile = Format(Date, "dd-mmm-yyyy")
ruta = Application.ActiveWorkbook.Path
ruta2 = ruta & "" & "export"
If Dir(ruta2, vbDirectory) = "" Then
MkDir ruta2
End If
ruta3 = ruta2 & "" & "Back up"
If Dir(ruta3, vbDirectory) = "" Then
MkDir ruta3
End If
ruta4 = ruta3 & "" & nFile
If Dir(ruta4, vbDirectory) = "" Then
MkDir ruta4
End If
Dim savedate
savedate = Date
Dim savetime
savetime = Time
Dim formattime As String
formattime = Format(savetime, "hh.MM.ss")
Dim formatdate As String
formatdate = Format(savedate, "DD - MM - YYYY")
Application.DisplayAlerts = False
Dim backupfolder As String
backupfolder = ruta4
ActiveWorkbook.SaveCopyAs Filename:=backupfolder & formatdate & " " & formattime & " " & ActiveWorkbook.Name
ActiveWorkbook.Save
End Sub
[/FONT]
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
It doesn't look like you're putting backslashes in between the folder names

ruta2 = ruta & "" & "export"
should be
ruta2 = ruta & "" & "export"

 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
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