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

  1. MsgBox

The following example illustrates the use of Msgbox

  1. Sub messagebox()
  2. MsgBox "This is a Message Box"
  3. End Sub

excel vba message box in vba

excel vba message box in vba

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.

  1. Sub Msgbox_buttons()
  2. If MsgBox("This is a Message Box", vbOKCancel) = vbOK Then
  3. MsgBox "OK clicked"
  4. Else
  5. MsgBox "Cancel Clicked"
  6. End If
  7. End Sub

Execute the above code and click on the buttons to see the result.

 

You can find similar Excel Questions and Answer hereunder

1) How can I get input from user through a prompt and assign user's input to a cell?

2) Write to text file without quotes in vba in Excel

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

4) Vba clear the contents of an entire sheet in Excel

5) How can worksheet functions be accessed in VBA?

6) How can I get row count of filtered data?

7) Import txt file in Excel

8) How can I prevent users from seeing / accessing my macro code?

9) How can I hide a sheet completely from users (the sheet should not even appear in Unhide dialog box)?

10) How can I set non-contiguous print area using VBA?

 

Here the previous and next chapter