Message box appearing twice when closing workbook

CaptainGravyBum

Board Regular
Joined
Dec 1, 2023
Messages
77
Office Version
  1. 365
Platform
  1. Windows
Hello,
I know this is going to be a real simple fix, but the answer is eluding me so would appreciate some help.
My excel workbook has some VBA script applied in the Workbook_BeforeClose section and when it runs, there is a message box that I need to display, but it appears twice.
I'm pretty sure it's because the message box appears on the initial close workbook is triggered and then again when the script closes the workbook to save changes, I'm hoping there is a way round this.

Code is as follows:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Sheets("Cross-Charge Request").Select
Range("E4,E5,E6,B8:F10").Select
Selection.ClearContents
Range("E4").Select
MsgBox "Cross Charge Request Form Updated"
Application.DisplayAlerts = False
ThisWorkbook.Close savechanges:=True
Application.DisplayAlerts = True

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try replacing this line:
VBA Code:
ThisWorkbook.Close savechanges:=True

with this:
VBA Code:
If Saved = False Then
    ActiveWorkbook.Save
End If

I think the above will address the actual issue and alternative would be to suppress the 2nd call.
VBA Code:
Application.EnableEvents = False
ThisWorkbook.Close savechanges:=True
Application.EnableEvents = True
 
Upvote 1
Solution

Forum statistics

Threads
1,224,820
Messages
6,181,162
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