How to Use the Excel VBA MsgBox Title – 5 Examples

This is an overview:

Overview of Excel VBA title


Excel VBA MsgBox

Anatomy of message box

Syntax of the VBA MsgBox Function in Excel

MsgBox( prompt [, buttons ] [, title ] [, helpfile, context ] )
  • prompt – shows the message.
  • [buttons] – The buttons to show:  Yes/No, Yes/No/Cancel, Retry/Ignore, etc.
  • [title] – What the message box is about. If there is no title, it will show Microsoft Excel.
  • [helpfile] – A helpfile is accessed when you use the Help button.
  • [context] – The Help context number is a numeric value assigned to a specific Help topic that can be used to identify and access the topic within the Help system.

Note: The MsgBox function can work with one argument: prompt


How to Launch the VBA Editor in Excel

If the Developer tab is not available on the ribbon, you need to enable it.

  • Go to the Developer tab and select Visual Basic.

How to create module

  • Select Insert,
  • Choose Module.

How to create module


Example 1 – General Message Box with Title

Overview of showing simple message with title

Create a MsgBox with the title: “It is the Title”.

  • In the VBA module, use the following code.

VBA code for Message title for simple Message box

Sub MsgBox_Title()
MsgBox "This is a sample box", , "It is the Title"
End Sub
  • Click Run or press F5.

Output of MsgBox with title It is the title

MsgBox has a Title argument that is changed to “It is the Title”.

In the MsgBox prompt [, buttons ] [, title ] [, helpfile, context ] function, the title is provided as “It is the title”.

Read More: How to Use MsgBox and InputBox in VBA Excel


Example 2 – Creating a Yes/No MsgBox with the Title Confirmation Window

Overview of Msg box with title Confirmation Window

  • In the VBA module, use the following code.

Code for the Confirmation Window

 

Sub Msgbox_continue()
result = MsgBox("Would you like to continue?", vbYesNo, "Confirmation Window")
End Sub
  • Click Run or press F5.

Output of Confirmation  Window

This code creates a Yes/No MsgBox with the title “Confirmation Window”.

Code Breakdown

result = MsgBox("Would you like to continue?", vbYesNo, "Confirmation Window")

 vbYesNo (the second argument, after the prompt) creates Yes and No buttons and the title: “Confirmation Window”.


Example 3 – Creating a Yes/No Warning MsgBox with Title

Overview of MsgBox with title Warning Message

  • In the VBA module, use the following code.

Code for the MsgBox with title Warning Message

 

Sub Msgbox_continue()
result = MsgBox("Would you like to continue?", vbYesNo + vbCritical, "Warning Message")
End Sub
  • Click Run or press F5.

Output  for the MsgBox with title Warning Message

The code creates a Warning MsgBox with title Warning Message.

Code Breakdown

result = MsgBox("Would you like to continue?", vbYesNo + vbCritical, "Warning Message")

vbYesNo+vbCritical creates Yes and No buttons and vbCritical creates the message box with the title: “Warning Message”.

Read More: Excel VBA: Show Multiple Values with Multiple Variables in MsgBox


Example 4 – Creating a Warning MsgBox with the Title “Failure” and Different Buttons

Overview of MsgBox with title Failure!

  • In the VBA module, use the following code.

Code of MsgBox with title Failure!

 

Sub Msgbox_continue()
result = MsgBox("Failed to Perform", vbAbortRetryIgnore + vbCritical, "Failure!")
End Sub
  • Click Run or press F5.

Output of MsgBox with title Failure!

Code Breakdown

result = MsgBox("Failed to Perform", vbAbortRetryIgnore + vbCritical, "Failure!")
  • vbAbortRetryIgnore creates the Abort , Retry and Ignore buttons.
  • vbCritical creates a warning MsgBox.
  • Failure! is the title.

Example 5 – Creating Multiple MsgBoxes with Different Titles

  • In the VBA module, use the following code.

Code of Creating a Working MsgBox with Title Capable of Performing Action

Sub Msg_Response()
Dim user_response As Integer
Dim message_prompt As String
Dim message_title As String
message_prompt = "Failed to Perform"
message_title = "Failure!"
user_response = MsgBox(message_prompt, vbAbortRetryIgnore + vbCritical, message_title)
Select Case user_response
Case 3
MsgBox "Action Aborted", vbInformation, "Abort Window"
Case 4
MsgBox "Retry Failed", vbInformation, "Retry Window"
Case 5
MsgBox "Error Ignored", vbInformation, "Ignore Window"
End Select
End Sub

 Code Breakdown

Dim user_response As Integer
Dim message_prompt As String
Dim message_title As String

A necessary variable is declared.

message_prompt = "Failed to Perform"
message_title = "Failure!"

The MsgBox title is assigned in the message_title variable.

user_response = MsgBox(message_prompt, vbAbortRetryIgnore + vbCritical, message_title)

Another variable is used to store the MsgBox command and the title.

Case 3
MsgBox "Action Aborted", vbInformation, "Abort Window"

“Case 3” If Abort is selected, the information MsgBox is displayed with the title Abort Window and with the prompt Action Aborted.

Case 4
MsgBox "Retry Failed", vbInformation, "Retry Window"

“Case 4” is the value for the selection of Retry. If Retry is selected, the information MsgBox is displayed with the title Retry Window.

Case 5
MsgBox "Error Ignored", vbInformation, "Ignore Window"

“Case 5” is the value for the selection of Ignore. If Ignore is selected, the information MsgBox is displayed with the title Ignore Window.

  • Click Run or press F5.

You will see the first box.

First output of the MsgBox with title Failure!

  • Click Abort to see the Information MsgBox below with the title Abort Window.

First output of the MsgBox with title Abort Windows after clicking Abort

  • If you select Retry, you will get another Information MsgBox named Retry Window.

First output of the MsgBox with title Retry Windows after clicking Retry

  • If you select Ignore, you will see another Information MsgBox named Ignore Window.

First output of the MsgBox with title Ignore Windows after clicking Ignore

 


Different Types of MsgBox Icons in Excel VBA

1. vbExclamation MsgBox in VBA

Overview of Exclamation MsgBox

The VBA code will create an Exclamation Box with the title “ExclamationTitle”.

  • Create a module and enter the code below.

Code of Exclamation MsgBox with title “ExclamationTitle”

Sub CustomMsgBoxwithExcaimation()
MsgBox "It is an Exclamation!", vbExclamation, "ExclamationTitle"
End Sub
  • Click Run or press F5.

Output of Exclamation MsgBox

 Code Breakdown

MsgBox "It is an Exclamation!", vbExclamation, "ExclamationTitle"

vbExclamation creates an Exclamation box.


2. vbQuestion MsgBox in VBA

Output of Question MsgBox

  • Create a module and enter the code below.

Question of Question MsgBox

 

Sub CustomMsgBoxwithQuestion()
MsgBox "It is a Question?", vbQuestion, "Question  Title"
End Sub
  • Click Run or press F5.

Output  of Question MsgBox

 Code Breakdown

MsgBox "It is a Question?", vbQuestion, "Question  Title"

vbQuestion creates a Question Message box in Excel.


How to Create VBA MsgBox with Different Types of Buttons

1. Message Box with OK and Cancel Button

Overview of OK and Cancel Buttonalt:Overview of OK and Cancel Button

  • Create a module and enter the code below.

Code of OK and Cancel Button

 

Sub CustomMsgBoxwithOKCancel()
MsgBox "Ok and Cancel is added", vbOKCancel, "OKCancel"
End Sub
  • Click Run or press F5.

Output of OK and Cancel Button

 Code Breakdown

MsgBox "Ok and Cancel is added", vbOKCancel, "OKCancel"

vbOKCancel creates an OK and a Cancel Button.


2. Message Box with Yes, No, and Cancel Button

Overview of MsgBox with Yes,No,Cancel Button

  • Create a module and enter the code below.

Code of MsgBox with Yes,No,Cancel Button

Sub CustomMsgBoxwithYesNoCancel()
MsgBox "Yes,No,Cancel  is added", vbYesNoCancel, "YesNoCancel"
End Sub
  • Click Run or press F5.

Result of MsgBox with Yes,No,Cancel Button

 Code Breakdown

MsgBox "Yes,No,Cancel  is added", vbYesNoCancel, "YesNoCancel"

vbYesNoCancel creates the Yes, No, and Cancel buttons.

Read More: Create VBA MsgBox Custom Buttons in Excel


List of Available Button Types for VBA MsgBoxes in Excel

There are other buttons available to use within VBA as well. Here is a list of the button types you can use either as one or in combination with each other in Excel.

BUTTON CODES VALUES DESCRIPTION
vbOKOnly 0 Shows the OK button only (Default).
vbOKCancel 1 Shows the OK and Cancel buttons.
vbAbortRetryIgnore 2 Shows the Abort, Retry and Ignore buttons.
vbYesNo 3 Shows the Yes and No buttons.
vbYesNoCancel 4 Shows the Yes, No and Cancel buttons.
vbRetryCancel 5 Shows the Retry and Cancel buttons.
vbMsgBoxHelpButton 16384 Shows the Help Button.
vbDefaultButton1 0 Defines the first button default.
vbDefaultButton2 256 Defines the second button default.
vbDefaultButton3 512 Defines the third button default.
vbDefaultButton4 768 Defines the fourth button default.
vbMsgBoxRight 524288 Right aligns the text.
vbMsgBoxRtlReading 1048576 Sets text reading from the right to the left like in Arabic and Hebrew languages.

Frequently Asked Questions (FAQ)

1. Can I use a variable in the title of a VBA MsgBox?

Answer: Yes, you can use a variable by concatenating the variable with the rest of the message using the “&” operator:

title = “Custom Title”
MsgBox “Hello world!”, vbOKOnly, title

2. How do I keep the user’s response to a VBA MsgBox?

Answer: The MsgBox function returns a value that corresponds to the button that the user clicked. Keep this value using a variable:

response = MsgBox(“Do you want to continue?”, vbYesNo)

3. What values can be returned by a VBA MsgBox?

Answer: The MsgBox function can return the following values: vbOK, vbCancel, vbAbort, vbRetry, vbIgnore, vbYes, vbNo. (depending on the clicked button).

4. How can I customize the buttons that are displayed in a VBA MsgBox?

Answer: Use the “Buttons” argument to specify the buttons to be displayed. For example MsgBox “Do you want to continue?”, vbYesNoCancel

5. How can I customize the icon that is displayed in a VBA MsgBox?

Answer: Use the “Icon” argument to specify the icon to be displayed. For example MsgBox “Error!”, vbCritical

6. Can I use line breaks in the message displayed by a VBA MsgBox?

Answer: Yes, use the vbCrLf constant to insert line breaks in the message displayed by a MsgBox. For example MsgBox “First line” & vbCrLf & “Second line”


Download Practice Workbook

Download the Excel workbook.


Related Article

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Joyanta Mitra
Joyanta Mitra

Joyanta Mitra, a BSc graduate in Electrical and Electronic Engineering from Bangladesh University of Engineering and Technology, has dedicated over a year to the ExcelDemy project. Specializing in programming, he has authored and modified 60 articles, predominantly focusing on Power Query and VBA (Visual Basic for Applications). His expertise in VBA programming is evident through the substantial body of work he has contributed, showcasing a deep understanding of Excel automation, and enhancing the ExcelDemy project's resources with valuable... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo