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
- vbAbortRetryIgnore
- vbCritical
- vbYesNo
- vbOkCancel
- vbOKOnly
- vbQuestion
- vbRetryCancel
and many more options are available.
The following code illustrates the vbYesNo button and its click event
- Sub msgboxyesnoevents()
- If MsgBox("Are you sure", vbYesNo) = vbYes Then
- MsgBox "its a YES"
- Else
- MsgBox "Its a NO"
- End If
- End Sub
The following code shows the OkCancel
- Sub msgboxokcancelevnts()
- If MsgBox("Are you sure", vbOKCancel) = vbOK Then
- MsgBox "its a OK"
- Else
- MsgBox "Its a CANCEL"
- End If
- End Sub
The screenshot of the code and the output are as shown below: