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
Search our website
We also send out useful tips in a monthly email newsletter ...
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.
This blog is part of our Excel macros online tutorial series. Our main business is running training courses in Excel, courses in VBA and training in many other Microsoft applications.
|
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
MsgBox _
prompt:="Cats like plain crisps", _
Buttons:=vbOKOnly, _
Title:="Shock claim"
This has the advantage of being easier to read - and you can put the arguments in any order.
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.