BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
The MsgBox command in Visual Basic for Applications allows you to display basic messages, but you can also make it prettier, and even use it to ask questions or display multiple-line messages! Read on to find out more about this versatile VBA command.
- Using MsgBox to Display Messages in VBA Macros
- Displaying Message Boxes (this blog)
- Joining Bits of a Message Together
- Customising your Message Box
- Using MsgBox to Ask Questions
Posted by Andy Brown on 28 September 2011
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Displaying Message Boxes
To display a message box, you'll need to know how it's made up, and the VBA syntax to display it on screen.
Parts of a Message Box
The component parts of a message box are shown below:
The parts of a message box are:
Syntax of a Message Box
The easiest way to see how to create a message box is from the Intellisense offered:
The list of arguments to the MsgBox command
The peculiar argument is Buttons, about which more later in this blog. You can display a message box either by listing the arguments in the right order, or by using named arguments - an example of each follows.
Displaying a Message Box using Positional Arguments
The easiest way to display a message box is by entering the arguments in the right order:
'display a message with an OK button and a title
MsgBox "Cats like plain crisps", vbOKOnly, "Shock claim"
In this case, the arguments are assumed to be in the right order, so we have:
|Argument||Name||What it means||Value here|
|1||Prompt||The message to be displayed||Cats like plain crisps|
|2||Buttons||The buttons, etc, to display||vbOkOnly|
|3||Title||The title for the mesage box||Shock claim|
Running this macro line would give:
The message box has all of the things we wanted: a prompt, an OK button and a title.
If you want to omit an argument using this method, you must include an extra comma. For example, the following message would not set any value for the Buttons argument:
MsgBox "Hello, World", , "Greeting"
Displaying a Message Box using Named Arguments
if you have the energy, it's better practice to use named arguments:
'display a message using named arguments
prompt:="Cats like plain crisps", _
This has the advantage of being easier to read - and you can put the arguments in any order.