×

Excel VBA MessageBox

Message Box

The MsgBox in Excel VBA is a dialog box used to inform the users of your program by showing a custom message or get some necessary inputs such as Yes/No or OK/Cancel. MsgBox is a VBA function and has a similar syntax as other VBA functions.

When the MsgBox dialog box is displayed, the VBA code is halted for that point of time. One needs to select any of the buttons in the MsgBox or need to click on the close icon to run the remaining VBA code.

Parts of VBA

Parts of VBA

Title: This is used to display what the message box is about. If you don’t specify anything in the title section, it displays the default Excel application name.

Prompt: It displays the message that the programmer wants to enter. You can use this space to write a couple of lines or even present tables/data here.

Button(s): One can customize the buttons to show buttons such as Yes/No, Yes/No/Cancel, Retry/Ignore, etc. The OK is the default Msgbox button.

Close Icon: This icon in Msgbox is used to close the message box. This is the same, unlike Microsoft's word, excel, etc.. Hence the user can close by clicking on the close icon.

Syntax of VBA

MsgBox( prompt [, buttons ] [, title ] [, helpfile, context ] )

Parameter

Prompt (required) – This parameter displays the message that you see in the MsgBox. One can use up to 1024 characters in the prompt parameter and can also use it to display the various values of variables.

Buttons (optional) – It determines what buttons and icons are displayed in the MsgBox. The buttons are logically divided into four groups. The first group is between 0 to 5, specifies the buttons to be displayed in the message box. The second group comprising of values 16, 32, 48, 64 defines the style of the icon to be displayed, the third group comprising of 0, 256, 512, 768 specifies the default button, and the fourth group (0, 4096) determines the modality of the message box. The available buttons are as follows: 

  • 0 vbOKOnly - Displays OK button only.
  • 1 vbOKCancel – This button format displays OK and Cancel buttons.
  • 2 vbAbortRetryIgnore – It is used to display Abort, Retry, and Ignore buttons.
  • 3 vbYesNoCancel - Displays Yes, No, and Cancel buttons.
  • 4 vbYesNo – It displays two buttons i.e., Yes and No.
  • 5 vbRetryCancel – It is used to displays Retry and Cancel buttons.
  • 16 vbCritical – It displays the Critical Message icon.
  • 32 vbQuestion – It is used to display the Warning Query icon.
  • 48 vbExclamation – This button displays the Warning Message icon.
  • 64 vbInformation – This button displays the information Message icon.
  • 0 vbDefaultButton1 – This format is used to make the first button is the default.
  • 256 vbDefaultButton2 - This format is used to make the second button is the default.
  • 512 vbDefaultButton3 - This format is used to make the third button is the default.
  • 768 vbDefaultButton4 - This format is used to make the fourth button is the default.

Title (optional) –This parameter is used to specify the caption you want to use in the message dialog box. The title is displayed at the top (title bar) of the MsgBox. If you don’t specify anything, by default it will show the name of the application.

Helpfile (optional)– This parameter is used to specify a help file that can be accessed whenever a user clicks on the Help button. The help button would appear only when the developer will use the button code for it. If the developer is using a help file, he also needs to specify the context argument.

Context (optional)– This parameter represents a numeric expression that is the Help context number assigned to the appropriate Help topic. These are rarely used in VBA.

Return

The MsgBox function returns integer values, which are used to identify the button the user has clicked in the message box. MsgBox function can return one of the following values:

  • 1 – This integer value is returned when the “vbOK” button (OK was clicked) is passed.
  • 2 - This integer value is returned when the “vbCancel” button (Cancel was clicked) is passed.
  • 3 - This integer value is returned when the “vbAbort” button (Abort was clicked) is passed.
  • 4 - This integer value is returned when the “vbRetry” button (Retry was clicked) is passed.
  • 5 - This integer value is returned when the “vbIgnore” button (Ignore was clicked) is passed.
  • 6 - This integer value is returned when the “vbYes” button (Yes was clicked) is passed
  • 7 - This integer value is returned when the “vbNo” button (No was clicked) is passed.

Example 1

Sub MsgBox_Exercise()
 MB = MsgBox("Do you like Excel?", vbYesNo)
     If MB = vbYes Then
         MsgBox "Wow! I also like working on Excel!"
     Else
         MsgBox "Ohh! Try once and you will surely like it."
     End If
 End Sub 

Output

Microsoft Excel

If the user clicks on yes

If the user clicks on yes

If the user clicks on No

If the user clicks on No

Example 2

Sub MsgBox_Excercise2()
 'button to implement cancel
 MsgBox "Welcome to VBA!", vbRetryCancel
 'button to implement retry and help options
 MsgBox "Welcome to VBA!", vbRetryCancel + vbMsgBoxHelpButton
 'button to implement yes and no options
 MsgBox "What do you want to do next?", vbYesNoCancel + vbDefaultButton2
 End Sub 

Output

welcome to VBA

Click any of the two options.

Click any of the three options.

Click any of the three options

Click any of the three options.


Related Topics

Excel VBA Functions

What is a function? A function is also called a procedure, but it is not a sub procedure, it’s a function procedure. You have already been using some function in Excel,...

7 minutes read.

Excel VBA Oct Function

VBA Oct Function: The Oct function in VBA converts the given number into octal notation and returns the result as a string. Syntax Oct (Number) Parameter Number (required) – This parameter represents the numeric value...

1 minute read.

Excel VBA CStr Function

VBA CStr Function: The CStr function in VBA converts an expression into a string data type. Syntax CStr (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to a...

1 minute read.

Excel VBA LBound Function

Excel VBA LBound Function: The LBound function in VBA returns the lowest subscript for the specified dimension in the given array. Syntax LBound (ArrayName, [Dimension]) Parameter ArrayName (required) – This parameter represents an array for which...

1 minute read.

Excel VBA Hour Function

The hour function in VBA returns the hour element for the specified time. Syntax Hour (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the hour element for the specified time. Example 1 Sub HourFunction_Example1() ...

1 minute read.

Excel VBA CByte Function

VBA CByte Function: The CByte function in VBA converts an expression into a Byte data type. Syntax CByte (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to convert to...

1 minute read.

Finding Last Row or Column in Excel VBA

Finding Last Row or Column in VBA Finding the last used row, column, or cell is one very commonly used task when we write macros and VBA applications.  Like other codes...

5 minutes read.

Excel VBA GoTo Statement

GoTo Statement he GoTo statement branches unconditionally to a specified line in a procedure. It is used to transfer the program control to a new statement, which is headed by a label. It sends...

2 minutes read.

Debugging in Excel VBA

Debugging in VBA: Debugging is a technique used to fix errors in programming languages. In Excel VBA, we have different ways by which you can identify the error in the...

2 minutes read.

VBA Pivot Table Grouping

VBA- Pivot Table Grouping For an instance, if in our pivot table, we have 11 different age groups from 20 to 30 -  but there might be a possibility that we...

2 minutes read.

Excel VBA Tutorial

What is VBA? Introduction to Excel VBA: Visual Basic for Applications (VBA) is a programming language developed by Microsoft to automate operations in applications, such as Excel, Word, PowerPoint, etc. It...

5 minutes read.

Excel VBA CLng Function

VBA CLng Function: The CLng function in VBA converts an expression into a Long data type. It returns a long value ranging between -2,147,483,648 and 2,147,483,647. Syntax CLng (Expression) Parameter Expression (required) – This...

1 minute read.

VBA Pivot Table

What is a Pivot Table? One of the most powerful features of Excel VBA is the Pivot Table. A pivot table is a VBA tool that is used to create summary...

3 minutes read.

Excel VBA Format Function

VBA Format Function The format function in VBA applies a specified format to an expression and returns the result as a string. Syntax Format (Expression, [Format], [FirstDayOfWeek] , [FirstWeekOfYear] ) Parameter Expression (required)- This parameter represents the expression that you want to format. Format...

3 minutes read.

Excel VBA MonthName Function

Excel VBA MonthName Function: The MonthName function in VBA returns a string with the month name for the specified month number. Syntax MonthName (Month, [Abbreviate]) Parameter Month (required) – This parameter represents an integer between...

1 minute read.

VBA Global Variable

What is Global Variable? The Global Variables in VBA refers to the variables declared before the start of any macro. They are defined outside the functions and are used by all the functions or...

6 minutes read.

Excel VBA StrComp Function

VBA StrComp Function: The StrComp function in VBA compares two strings and returns an integer value displaying the result of the comparison. Syntax StrComp (String1, String2, [Compare]) Parameter String1 (required)- This parameter represents the first string to...

2 minutes read.

VBA Dim

What is Dim? DIM or Dimension or Declare in Memory is a keyword that is used in VBA to declare a variable with the different data types (Integer, String, variable, Boolean, Double, etc.)...

7 minutes read.

Excel VBA CDate Function

VBA CDate Function: The CDate function in VBA converts an expression into a Date (or Time) data type. Syntax CDate (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to...

1 minute read.

Procedures in VBA

Procedures in VBA A procedure is a block of statements or units of computer code that performs some action. It is enclosed with a declaration statement, and its primary purpose is to carry out...

3 minutes read.