Vba message box in vba in Excel
The MessageBox is used to display data or message to the users.
The most common usage is to display warning messages to the users.
But, Msgbox can also be used to check the result of a particular variable so as to spot the error in large programs
In financial applications involving hundreds of lines of code with various dependent variables, the Msgbox can be helpful to check the correctness of any variable before analyzing the correctness of the result
Syntax
- MsgBox
The following example illustrates the use of Msgbox
- Sub messagebox()
- MsgBox "This is a Message Box"
- End Sub
Now, Consider a scenario, where the further execution of a script is based on decision of the user.
In this case, we can use Msgbox with "OK" and "Cancel" button and proceed further based on the button being clicked.
The following example illustrates this feature.
- Sub Msgbox_buttons()
- If MsgBox("This is a Message Box", vbOKCancel) = vbOK Then
- MsgBox "OK clicked"
- Else
- MsgBox "Cancel Clicked"
- End If
- End Sub
Execute the above code and click on the buttons to see the result.