559 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Using MsgBox to Display Messages in VBA Macros
Part two of a five-part series of blogs
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.
To display a message box, you'll need to know how it's made up, and the VBA syntax to display it on screen.
The component parts of a message box are shown below:
The parts of a message box are:
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.
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"
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.
|Parts of this blog|
25 Aytoun Street