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.

  1. Using MsgBox to Display Messages in VBA Macros
  2. Displaying Message Boxes (this blog)
  3. Joining Bits of a Message Together
  4. Customising your Message Box
  5. Using MsgBox to Ask Questions

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.

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:

Parts of a message box

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:

Arguments to MsgBox

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:

Resulting message box

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

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.

This blog has 0 threads Add post