How to Use Message Boxes in Excel: Step-by-Step Guide (MsgBox)

In this article, we are going to show you how to create a message box in Excel.

Also called an Excel message box (VBA style).

If you do not have any experience with VBA, do not worry (make sure to check this out too).

This tutorial will be a great introduction for you!

Soon you can impress your friends and colleagues. Furthermore, you may find some cool uses for your new-found message box skills.

If you want to tag along with me, download the sample workbook here.

What is an Excel VBA message box?

A VBA message box is a pop-up-style dialog box that you can program the behavior of using VBA code.

It provides an elegant and sometimes necessary way for the end user to interact with a workbook.

It can be something as simple as an alert to the user. It could be something more complex that requires action by the user to proceed.


Like functions and formulas in Excel, the MSGBOX function is how we create a message box using VBA code.

Before we start writing VBA code, let’s take a quick look at the syntax requirements for the MSGBOX function.

The syntax of the MSGBOX function

=MSGBOX(Text_String , [buttons] ,[title], [helpfile, context])

The first and only required argument is Text_String. This is the message text.

It is the text string you want the message box to say to the user. You must enclose the text string in double quotes and limit it to 1,024 characters.

The buttons argument is optional but can you can use it to make your message box interactive. This argument is not an arbitrary value.

There are 20 available button parameter constants available in Excel.

vba msgbox function parameters

If you omit the button argument, the default is of type vbOkayOnly.

The next argument is the title parameter. It is also optional.

This argument assigns a custom title to the message box. It is a string expression that displays in the title bar of the dialog box.

If omitted, the title bar will display the application name.

The helpfile argument is a string parameter and it is optional. It becomes required if you use the context parameter.

The helpfile argument specifies the help file for use with the message box.

The final argument, context, is a numeric parameter. This argument specifies the number assigned to the appropriate Help topic.

The context argument is an optional parameter unless there is a helpfile argument.

Message box function example #1: Basic concept

Let’s create our first message box. In this example, we just want to create a dialog box to say “Hello world!”

The first thing we need to do is click on Visual Basic in the Code group on the Developer tab.


You can also press Alt + F11 to get the same result.

You should now have the VB Editor open. In Project Explorer, you will find your workbook file listed.

Expand the contents of the workbook and right-click on Microsoft Excel Objects. Hover your cursor over Insert and select Module from the menu that appears.


This will create a new blank module.

blank module to insert excel vba msgbox

Now we can start creating our first message box using Excel VBA code.

Kasper Langmann, Microsoft Office Specialist

At the top of the code window, type “Sub firstMessage()” (without the quotes). Notice that Excel adds the line “End Sub” below this line when you press Enter.

All the code we will type next must be between these two lines.

Blank macro in Excel vba module

This is where we begin typing our MsgBox function.

excel vba message box function syntax

Note that when we type MsgBox, the syntax help appears.

Kasper Langmann, Microsoft Office Specialist
first vba msgbox hello world example with ok button

Now we can run the macro by clicking the Run Sub button (green “play” button) or pressing F5.

excel vba msgbox: running the simple message box

This will cause our message box to appear.

excel vba message box with ok button (default button)

Note that the default button is “OK” only.

Also, the title is the default showing the application name (“Microsoft Excel”).

Message box function example #2: Message box title

Let’s continue to work with the same code by copying and pasting it to a new subroutine.

We will tweak the sub name and add a message box title to replace the default in our message box.

We have to place two commas between our Text_string argument and title argument. This is because we are omitting the ‘button’ argument.

example of vba msgbox with custom title

Now we get a message box with our new custom title.

vba msgbox prompt with hello world

Message box function example #3: Multiple buttons

Now let’s try a different button argument than the simple default button from the example above.

Instead of the default vbOKOnly, we will select vbYesNoCancel. In the process, we will also change our Text_string argument to a question for the sake of relevance.

This changes the msgbox prompt a lot as it gives the user the option to opt out of whatever’s happening with cancel buttons.

vba msgbox button constants including cancel buttons and vbyesno

This code renders the following message box with both a Yes, No, and Cancel buttons.

excel vba msgbox prompt with yes no and cancel buttons instead of just default button

Message box function example #4: Multiple lines in VBA msgbox

You can also create multiple lines of text in a message box using the vbNewLine character.

This VBA code added to the msgbox function gives both a line break:

“& vbNewLine &”

And the vbYesNo adds a little more than the one default OK button.

vba msgbox with vbnewline to insert a line break

This renders the following message box.

excel vba vbyesno message box

Message box function example #5: Advanced logic with the “if” statement

In our final example, we are going to take the message box concept to the next level.

We will use an “if” statement to generate a different message box.

This relies on a “Yes” response or “No” response for the first message box in the subroutine.

Using if logic to control the message box window

This code is a bit more complex than what we have been working with.

It triggers a different message box for each of the possible responses to the first message box.

If the user clicks “Yes” the subroutine will open the following message box.

vba msgbox prompt that asks to proceed with the OK button.

If the user clicks “No” the sub-routine will generate the following message box.

vba msgbox with ok button

This example starts to scratch the surface of how you can use a message box to direct the flow of your macros.

Kasper Langmann, Microsoft Office Specialist

This can get much more complex if you decide to continue your VBA and message box skill development.

That’s it – Now what?

You just learn to show and control a message box window using the VBA MsgBox function.

But message boxes are not just awesome programming party tricks 🥳

Yes, you can use them to impress your friends and colleagues.

But it is also a very useful tool for creating interactivity and control for your workbooks.

Although message boxes are nice, they’re just a tiny part of VBA code as a whole.

To learn more, enroll in my free 30-minute VBA course here.

Other resources

If you’re not well-versed in the Visual Basic Editor yet, read my guide to it here.

Also, at some point, you definitely don’t want to miss out on using IF statements and Loops in your Excel VBA code.