Workbook slow to close - any obvious issues with the code?

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi

This is my workbook_before close event code.

I'd be grateful to know if the 20 seconds or so it takes to close can be expected with the below code or if there's something obvious that can be amended to speed it up?

Many thanks!

VBA Code:
Private Sub workbook_beforeclose(Cancel As Boolean)

Dim MsgResult As Integer

Application.EnableEvents = False

MsgResult = MsgBox("Are you SURE you want to overwrite the master Exercise Log file?   ", vbYesNoCancel + vbExclamation, "WARNING")

Select Case MsgResult

    Case vbNo
        'a) If you select No, you get a second dialog box saying "Existing file unchanged" and the workbook as well as Excel will close.
        
        'checks to see if other workbooks are open.  It will not shut down the application if there are other workbooks open
        If Application.Workbooks.Count < 2 Then
            MsgBox "Master file unchanged - data NOT saved" & vbNewLine _
            & "" & vbNewLine _
            & "Exercise Log will now close", vbInformation, "Master File Unchanged "
            'the following line does not save the workbook but sets a bit that tells Excel that any changes have already been saved, even if the changes were not actually saved
            ThisWorkbook.Saved = True
            Application.Quit
        Else
            'other workbooks are open.  Leave the application alone and
            'simply close this workbook.
            MsgBox "Other workbooks open - data NOT saved!" & vbNewLine _
            & "" & vbNewLine _
            & "Exercise Log will now close!", vbInformation, "Master File Unchanged "
            Me.Close False
        End If
        
    Case vbCancel
        'b) If I select Cancel then there's no action, the dialog box closes and the workbook remains open as if nothing had happened.
        
        Cancel = True
        'simply cancel the closing of this workbook
         
    Case vbYes
        'c) If I select Yes, then the new data is saved, a second dialog box appears + vbconf with existing data overwritten.
        '   The file and Excel then close.
   
'Bernie Dietrick
Dim bdFileName As String
Dim FullFileName  As String

Application.DisplayAlerts = False

FullFileName = ActiveWorkbook.FullName
bdFileName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)

If LCase(Application.UserName) <> "jsullivan" Then
   ActiveWorkbook.SaveCopyAs Filename:="E:\BACKUPS\Exercise Log\" & _
      bdFileName & " Backup - " & Format(Now, "dddd dd mmmm yyyy, h.mmam/pm") & _
      ".xlsm"

   'Backup to 128gb USB drive as well
   ActiveWorkbook.SaveCopyAs Filename:="Y:\DOCUMENTS\EXERCISE LOG\Exit Backups\" & _
      bdFileName & " Backup - " & Format(Now, "dddd dd mmmm yyyy, h.mmam/pm") & _
      ".xlsm"

End If

ActiveWorkbook.SaveAs Filename:=FullFileName, AddToMru:=False

Application.DisplayAlerts = True

        Worksheets("Training Log").[H1:H9] = vbNullString
        'Worksheets("Analysis").[G1:Z1] = vbNullString
        'Worksheets("Iron Man Log").[G1:Z1] = vbNullString
        Worksheets("Daily Tracking").[CI1:CZ1] = vbNullString
        Worksheets("Indoor Bike").[I1:I2] = vbNullString
        
        'Application.EnableEvents = True 'commented out 20.09.2021
        MsgBox "New backup files created in" & vbNewLine & vbNewLine _
        & "E:\BACKUPS\EXERCISE LOG    " & vbNewLine & vbNewLine _
        & "Y:\DOCUMENTS\EXERCISE LOG\EXIT BACKUPS" & vbNewLine & vbNewLine _
        & "Exercise Log will now close", vbInformation, "Master File Overwritten"
        
         ThisWorkbook.Saved = True
         'Application.EnableEvents = True
        
End Select

Application.EnableEvents = True

End Sub
 
I welcome those, they're not a problem for me, they're my comfort blanket :biggrin:
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Why do you think this line was created for me? Isn't that the Excel default?
VBA Code:
ActiveWorkbook.SaveAs Filename:=FullFileName, AddToMru:=False
Yes, AddtoMru:=False is the default, so no need for it.



See how the following works for you:

VBA Code:
Private Sub workbook_beforeclose(Cancel As Boolean)
'
    Dim MsgResult As Long
'
'   Turn Settings off
      Application.ScreenUpdating = False                                    ' Turn Screen Updating off
         Application.Calculation = xlCalculationManual                      ' Turn AutoCalculation off
        Application.EnableEvents = False                                    ' Turn EnableEvents off
'
    MsgResult = MsgBox("Are you SURE you want to overwrite the master Exercise Log file?   ", vbYesNoCancel + vbExclamation, "WARNING")
'
    Select Case MsgResult
        Case vbNo
'           a) If you select No, you get a second dialog box saying "Existing file unchanged" and the workbook as well as Excel will close.
'
'           checks to see if other workbooks are open.  It will not shut down the application if there are other workbooks open
            If Application.Workbooks.Count < 2 Then
                MsgBox "Master file unchanged - data NOT saved" & vbNewLine & "" & vbNewLine & "Exercise Log will now close", vbInformation, "Master File Unchanged "
'
'               the following line does not save the workbook but sets a bit that tells Excel that any changes have
'                   already been saved, even if the changes were not actually saved
                ThisWorkbook.Saved = True
'
                Application.Quit
            Else
'               other workbooks are open.  Leave the application alone and simply close this workbook.
                MsgBox "Other workbooks open - data NOT saved!" & vbNewLine & "" & vbNewLine & "Exercise Log will now close!", vbInformation, "Master File Unchanged "
'
                Me.Close False
            End If
        Case vbCancel
'           b) If I select Cancel then there's no action, the dialog box closes and the workbook remains open as if nothing had happened.
            Cancel = True                                   'simply cancel the closing of this workbook
        Case vbYes
'           c) If I select Yes, then the new data is saved, a second dialog box appears + vbconf with existing data overwritten. The file and Excel then close.
'           Bernie Dietrick
            Dim bdFileName      As String
            Dim FullFileName    As String
'
            Application.DisplayAlerts = False
'
            FullFileName = ActiveWorkbook.FullName
            bdFileName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
'
            If LCase(Application.UserName) <> "jsullivan" Then
''                ActiveWorkbook.SaveCopyAs Filename:="E:\BACKUPS\Exercise Log\" & bdFileName & " Backup - " & Format(Now, "dddd dd mmmm yyyy, h.mmam/pm") & ".xlsm"
                ActiveWorkbook.SaveCopyAs Filename:="E:\BACKUPS\Exercise Log\" & bdFileName & " Backup - " & Format(Now, "dddd dd mmmm yyyy, h:mm am/pm") & ".xlsm"
'
'               Backup to 128gb USB drive as well
''                ActiveWorkbook.SaveCopyAs Filename:="Y:\DOCUMENTS\EXERCISE LOG\Exit Backups\" & bdFileName & " Backup - " & Format(Now, "dddd dd mmmm yyyy, h.mmam/pm") & ".xlsm"
                ActiveWorkbook.SaveCopyAs Filename:="Y:\DOCUMENTS\EXERCISE LOG\Exit Backups\" & bdFileName & " Backup - " & Format(Now, "dddd dd mmmm yyyy, h:mm am/pm") & ".xlsm"
            End If
'
''            ActiveWorkbook.SaveAs Filename:=FullFileName, AddToMru:=False
            ActiveWorkbook.SaveAs Filename:=FullFileName
'
            Application.DisplayAlerts = True
'
            Worksheets("Training Log").[H1:H9] = vbNullString
            Worksheets("Daily Tracking").[CI1:CZ1] = vbNullString
            Worksheets("Indoor Bike").[I1:I2] = vbNullString
'
            MsgBox "New backup files created in" & vbNewLine & vbNewLine & "E:\BACKUPS\EXERCISE LOG    " & vbNewLine & vbNewLine _
                & "Y:\DOCUMENTS\EXERCISE LOG\EXIT BACKUPS" & vbNewLine & vbNewLine & "Exercise Log will now close", vbInformation, "Master File Overwritten"
'
            ThisWorkbook.Saved = True
    End Select
'
'   Turn Settings back on
    Application.EnableEvents = True                                                             ' Turn EnableEvents back on
    Application.Calculation = xlCalculationAutomatic                                            ' Turn AutoCalculation back on
    Application.ScreenUpdating = True                                                           ' Turn Screen Updating back on
End Sub
 
Upvote 0
Many thanks Johnny.

I don't know why I'm getting error 1004 with this line though because I haven't changed the save path?
VBA Code:
ActiveWorkbook.SaveCopyAs Filename:="E:\BACKUPS\Exercise Log\" & bdFileName & " Backup - " & Format(Now, "dddd dd mmmm yyyy, h:mm am/pm") & ".xlsm"

Also, I don't think I need this line
VBA Code:
If LCase(Application.UserName) <> "jsullivan" Then
As it's not me, it was somebody who was testing this for me many years ago
 
Upvote 0
You have a slash and a colon in the time format which are illegal characters for filenames in Windows.

Naming Files, Paths, and Namespaces - Win32 apps

< (less than)
> (greater than)
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
 
Upvote 0
Upvote 0
Thanks Gokhan. I don't understand because that line has been exactly the same for the last 15 years and has made backups every time I've closed the workbook, with no 1004 error.

Respectfully, have you tested that line yourself? If it does error for you then I'd be amazed why it hasn't for me!

Thanks again.
 
Upvote 0
Ok, just came home.

1. Filename does not have a slash as it's either am or pm. That was a bit misleading on my part, sorry for that.

I do get run-time 1004 though with the following:

VBA Code:
ActiveWorkbook.SaveCopyAs Filename:="D:\Backup - " & Format(Now, "dddd dd mmmm yyyy, h:mm am/pm") & ".xlsm"

1632233352920.png

If I change the colon to a period, it saves alright.
 
Upvote 0
Ah, OK
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)
I wonder why it works for me but not you? I'm on Windows/Office 64 bit and maybe you're not, I wonder if that's the reason?

Anyway, I changed them both to a dot and...your amendments have reduced the close time by at least 10 seconds, so THANK YOU!

Is it possible you could just change the order of the 2 msgboxes so this one runs last please? It just seems illogical that I'm being asked if I want to save the file after backups of it have been created.
VBA Code:
            MsgBox "New backup files created in" & vbNewLine & vbNewLine & "E:\BACKUPS\EXERCISE LOG    " & vbNewLine & vbNewLine _
                & "Y:\DOCUMENTS\EXERCISE LOG\EXIT BACKUPS" & vbNewLine & vbNewLine & "Exercise Log will now close", vbInformation, "Master File Overwritten"
Even though it's below the other msgbox in the code, I can't work out why it pops up first and I don't want to mess up the good work you've just done!

Thanks again Gokhan!

Edit - I've just looked closely at the saved files and the file names were always saved in the non-colon format despite the colon in the 2 save names, so I'm guessing Windows or Excel must have semi-hung while hesitating with the colons?
 
Last edited:
Upvote 0
Both 64-bit here.

I took that part from StackOverflow, so the comment is from someone else. However, it may be that your previous E and Y folders were on a non NTFS partition, like FAT32 perhaps?

Can you post the final code you have at the moment please?

Also, another idea, I wonder if you first save the workbook and then copy paste it to the 2 folders while renaming would be faster... (Just as you thought this was over :p)
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,660
Latest member
Zatman

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