msgbox "X" button

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Greeting, tried searching for this, but I can't find the answer - probably because I am not asking the question correctly... What is the name for the little "x" close button in the top right of a msgbox?

Just ran across something that either I have never noticed before or perhaps I just have never clicked on it before (though I am sure that I must have by now :/) If I run the little sub below and I decide that I don't want to run the macro, if I click on the "x" the macro runs anyway instead of closing out the msgbox. I know that I could just add a cancel or no button to the msgbox, but that still would not prevent the issue if the user clicks the x. I guess I always thought that the "x" was a default cancel event but I guess not. What is the syntax that I need to add so that clicking the "x" stops the macro and closes the sub?

Thanks


Code:
Sub EmergencyLightClearForm()


MsgBox "Are you sure that you want to clear the data from this report?  This cannot be undone"


Worksheets("Emergency Lighting Inspection").Activate
    Range("E4:K17").Select
    Selection.ClearContents
    Range("P4:V61").Select
    Selection.ClearContents
    Range("Z4:AF17").Select
    Selection.ClearContents
    Range("AJ4:AP17").Select
    Selection.ClearContents
    Range("A1").Select
    
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Because you are not using a message box that allows you to return a value other than 1(one) and you are not creating an if statement that will help you exit the subroutine.

The "vbOKCancel" will make "Cancel"(or "x") =2 while "OK"=1

Code:
Sub EmergencyLightClearForm()
 
Dim ans As Single
 
ans = MsgBox("Are you sure that you want to clear the data from this report?  This cannot be undone", vbOKCancel)

If ans = 1 Then
    Worksheets("Emergency Lighting Inspection").Activate
        Range("E4:K17").Select
        Selection.ClearContents
        Range("P4:V61").Select
        Selection.ClearContents
        Range("Z4:AF17").Select
        Selection.ClearContents
        Range("AJ4:AP17").Select
        Selection.ClearContents
        Range("A1").Select
End If
    
End Sub
 
Upvote 0
Maybe:

Code:
Sub EmergencyLightClearForm()

    If MsgBox("Are you sure that you want to clear the data from this report?  This cannot be undone", _
                vbOKCancel, "Warning!") = vbOK Then
    
        Worksheets("Emergency Lighting Inspection").Activate
        Range("E4:K17, P4:V61, Z4:AF17, AJ4:AP17").ClearContents
        Range("A1").Select
    End If
    
End Sub
 
Upvote 0
Thank you both for these improvements to my code, and more importantly the knowledge that you have shared with me, I really appreciate the tutoring. It makes sense about me not making it an "if" statement being the problem - Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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