Macro to suppress also messages when closing workbooks

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have written code to close all workbooks except the current workbook where I run the macro

The Macro closes all the workbooks, but I want to suppress all messages including any message box that pops up in the workbook to be closed


Code:
 Sub CloseOtherWorkbooks()
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
Dim WB As Workbook
For Each WB In Workbooks
     If Not (WB Is ActiveWorkbook) Then WB.Close SaveChanges:=False
     With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
Next
End Sub


the following macro is placed in the code in the sheet on the source workbook to be closed, which I must suppressed in the macro above when closing as the files do not need not be saved

Code:
 Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.CutCopyMode = False
    
   If MsgBox("Have you entered the month and Year?", vbYesNo + vbQuestion) = vbNo Then
            Sheets("Summary").Select
            Cancel = True
        End If
    
     Application.CutCopyMode = False
End Sub



your assistance in resolving this is most appreciated
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Re: Macro to suppress also messages whern closing workbooks

Thanks. I found the code using the link and it worked
 
Upvote 0
Re: Macro to suppress also messages whern closing workbooks

Glad it did the job for you.
 
Upvote 0

Forum statistics

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