I am getting an error when I am attempting to close a backup file I created in my Save and Close Module

tewell53

New Member
Joined
Sep 11, 2018
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I am wanting to create a backup file to my backup directory the close that file. I then want to save and the close the current workbook. Then close the Excel Application. I am having trouble closing the backup file and at the end closing Excel.
VBA Code:
'/-------------------------------------------------------------
'/ Main total worksheet(23-24)  save and close button
'/-------------------------------------------------------------


Sub SaveAndClose()
    Dim BUFileName As String                                                                     '/ Declaring file path name
    Dim MyName As String                                                                       '/ Declaring file name
    Current_date = Format(Now, "MMDDYY_hhmmss")                                                '/ Assigning date & Time to use in file name

'/---------------------------------------------------------------
'/ Turning off notifications for savings and closing
'/---------------------------------------------------------------
    Application.DisplayAlerts = False
    Application.EnableEvents = False
'/---------------------------------------------------------------
'/ Creating a backup file and then closing it
'/---------------------------------------------------------------
    MyName = ActiveWorkbook.Name                                                               '/ Assigning file name of current active workbook
    BUFileName = "G:\TECH\ib271t0o\MyBackups\" & MyName & "-" & Current_date & ".xlsm"           '/ Buliding full path name for backup file
    ActiveWorkbook.SaveAs FileName:=BUFileName                                                   '/ Saving backup file
    Workbooks("G:\TECH\ib271t0o\MyBackups\" & MyName & "-" & Current_date & ".xlsm" ).Close
'/---------------------------------------------------------------
'/ Turning off notifications for savings and closing
'/ Saving any changes
'/ closeing this workbook
'/---------------------------------------------------------------
   ThisWorkbook.Save
   Application.Quit
   Application.DisplayAlerts = True
   Application.EnableEvents = True
    
 End Sub

All help is greatly appreciated.

Terry
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi
untested but see if this update to your code does what you want

VBA Code:
'/-------------------------------------------------------------
'/ Main total worksheet(23-24)  save and close button
'/-------------------------------------------------------------


Sub SaveAndClose()
    Dim BUFileName  As String        '/ Declaring file path name
    Dim MyName      As String, Current_Date As String        '/ Declaring file name variables
    
    Current_Date = Format(Now, "MMDDYY_hhmmss")        '/ Assigning date & Time to use in file name
    
    On Error GoTo myerror:
    '/---------------------------------------------------------------
    '/ Turning off notifications for savings
    '/---------------------------------------------------------------
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    
    ThisWorkbook.Save
    '/---------------------------------------------------------------
    '/ Creating a backup file
    '/---------------------------------------------------------------
    MyName = ThisWorkbook.Name        '/ Assigning file name of current active workbook
    BUFileName = "G:\TECH\ib271t0o\MyBackups\" & MyName & "-" & Current_Date        '/ Buliding full path name for backup file
    ThisWorkbook.SaveCopyAs Filename:=BUFileName        '/ Saving backup file
    
    '/---------------------------------------------------------------
    '/ Turning on notifications and closing
    '/---------------------------------------------------------------
    
myerror:
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    
    If Err <> 0 Then
        MsgBox (Error(Err)), 48, "Error"
     Else
        ThisWorkbook.Close False
  End If
    
    'Personally, I WOULD NOT recommend that you do this.
    'Application.Quit
    
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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