Macro to suppress also messages when closing workbooks

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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