Vba msgbox click events in Excel

In the previous chapter, we saw the application of a messagebox and its way of use.

The messagebox is used to display some message to the user.

The message box comes with a default OK button to close the message.

But, there are situations, when an application has to execute based on the user's response to a messagebox's information.

For example, a message box showing, OK and Cancel button; the execution of the program depends on the button being clicked.

So, a message box also can be used with events, which are as described below.

Various button choices of messagebox

  1. vbAbortRetryIgnore
  2. vbCritical
  3. vbYesNo
  4. vbOkCancel
  5. vbOKOnly
  6. vbQuestion
  7. vbRetryCancel

and many more options are available.

The following code illustrates the vbYesNo button and its click event

  1. Sub msgboxyesnoevents()
  2. If MsgBox("Are you sure", vbYesNo) = vbYes Then
  3. MsgBox "its a YES"
  4. Else
  5. MsgBox "Its a NO"
  6. End If
  7. End Sub

The following code shows the OkCancel

  1. Sub msgboxokcancelevnts()
  2. If MsgBox("Are you sure", vbOKCancel) = vbOK Then
  3. MsgBox "its a OK"
  4. Else
  5. MsgBox "Its a CANCEL"
  6. End If
  7. End Sub

The screenshot of the code and the output are as shown below:

excel vba msgbox click events

excel vba msgbox click events

excel vba msgbox click events


You can find similar Excel Questions and Answer hereunder

1) Here some explanations about the MSXML who stands for Microsoft XML core services

2) How do I disable the right click option for users in my WorkBook?

3) how do you enter multiple line in a cell in Excel

4) Here an explanation about list box and how to control list boxes in Excel VBA

5) Here an explanation about how to control the folder dialog that will let you access to your files using VBA

6) What are events in excel VBA. Here an explanation about VBA events.

7) How to use data table function in Excel. It helps you go through various scenario when seeking a goal

8) How can I get users to select a file for processing using my macro?

9) How to use the scenario manager in Excel

10) What are slicers, how work with slicers in Excel, how to filter data visually


Here the previous and next chapter