Lesson#119: What is a Message Box in VBA?
What is a Message Box?
A message box is a special dialog box used to display a piece of information to the user. The user cannot type anything in the message box. There are usually two kinds of message boxes you will create: one that simply displays information and one that expects the user to make a decision.
A message box is created using the MsgBox function. Its syntax is:
MsgBox(Prompt[, Buttons] [, Title] [, Helpfile, Context]) As VbMsgBoxResult
The MsgBox() function takes five arguments and only the first one is required.
Here is an example of how can you create a simple message box by writing code in the VBA editor.
MsgBox (“Your system is being scanned”)
End Sub
This will display a message like this.
The Prompt argument can be made of up to 1024 characters. To display the Prompt on multiple lines, you should use the constant vbCrLf
Here is an example:
MsgBox (“Your system is being scanned” & vbCrLf & “It will be reboot after scanning”)
End Sub
It shows a message box like this.
Configuring buttons of Message Box
The button’s argument specifies what button(s) should display on the message box. There are different types of buttons available in VBA
Here is an example of how to call a message box with a button.
ActiveCell = MsgBox(“Your system is being scanned.” & vbCrLf & “Would you like to reboot after scanning?”, vbYesNo)
End Sub
It shows a message box like this.
Configuring icons of Message Box
You can configure icons in Message Box to make it more appealing to users.
Here is an example of a Message box with an icon.
ActiveCell = MsgBox(“Your system is being scanned.” & vbCrLf & “Would you like to reboot after scanning?”, vbYesNo Or vbQuestion)
End Sub
It shows a message box like this.
Title of Message Box
Now is an example of a message box in which the title is configured.
ActiveCell = MsgBox(“Your system is being scanned.” & vbCrLf & “Would you like to reboot after scanning?”, vbYesNo Or vbQuestion, “System Message”)
End Sub
Here is how the message box shows.
The returned value of Message Box
the MsgBox() function returns a value by default which is defined in VBA language when the user presses a button. The values are these which are returned by buttons.
- helpfile − An optional parameter and a string expression that identifies the helpfile to provide context help for the dialogue box.
- context − An optional parameter and a numeric expression that identifies the help context number assigned by the author to the appropriate help topic.
Leave a Reply