×

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 InStr Function

VBA InStr Function The InStr function in VBA searches for a substring inside the given string. It returns the position of a substring within a string, as an integer if the substring is found...

1 minute read.

Excel VBA Sin Function

VBA Sin Function: The Sin function in VBA returns the sine value for a supplied angle. Syntax Sin (Number) Parameter Number (required) –This parameter represents the angle (in radians) to calculate the sine value. Return This function...

1 minute read.

Excel VBA Right Function

VBA Right Function: The Right function in VBA returns a substring from the end of the given string. Syntax Right (Str, Length) Parameter Str (required) – This parameter represents the string from which you want to...

1 minute read.

Excel VBA CVErr Function

VBA CVErr Function: The CVErr function in VBA returns an Error data type, involving with a user-specified error code. Syntax CVErr (Expression) Parameter Expression (required)- This parameter represents the required error code. Return This function returns an...

1 minute read.

Excel VBA User-Defined Functions

User-Defined Functions One of the advantages of VBA is that you can create your own functions using macros. These functions can be called and used as other functions in excel and use them. You can...

3 minutes read.

Excel VBA Conditional Statement

Conditional Statement in VBA Excel Conditional Statements in Excel VBA are one of the most powerful and useful features in programming, this will give you to perform comparisons to decide or...

2 minutes read.

VBA Charts

What is a Chart? A chart is used to visually show numbers or data in a spreadsheet (or spread over multiple spreadsheets) so that the end-user can look at the chart...

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.

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- Pivot Table Fields

VBA- Pivot Table Fields: The Pivot Fields collection contains all the fields from the data source, including any calculated fields. The main aspect of adding a field is its Position...

4 minutes read.

Excel VBA IsEmpty Function

VBA IsEmpty Function: The IsEmpty function in VBA returns a Boolean value showing whether the specified Expression is Empty (variant has not been declared) or not. Syntax IsEmpty (Expression) Parameter Expression (required)- This parameter...

1 minute read.

Excel VBA FormatPercent Function

VBA FormatPercent Function: The FormatPercent function in VBA is used to apply a percent format to a numeric expression, and it returns the result as a string. Syntax FormatPercent (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 minutes read.

Excel VBA Weekday Function

Excel VBA Weekday Function: The TimeValue function in VBA returns an integer (1 to 7), signifying the day of the week for the specified date. Syntax Weekday (Date, [FirstDayOfWeek]) Parameter Date (required) – This parameter...

2 minutes read.

Excel VBA AutoFilter

One of the reasons for Excel VBA’s popularity is its capability to filter and analyze data from huge database with the help of a method known as AutoFilter. This method permits a...

4 minutes read.

Steps to Create a Chart in VBA

Steps to Create a Chart Charts are created either by directly working with the chart variable object that defines the chart data or by ChartObject method. In order to get to...

3 minutes read.

Excel VBA Atn Function

VBA Atn Function: The Atn function in VBA returns the arctangent between quadrant -?/2 and +?/2 for the specified number, in radians. Syntax Atn (Number) Parameter Number (required) – This parameter represents the number that...

1 minute read.

Excel VBA Array Function

VBA Array Function: The Array function in VBA generates an array containing the given set of values. Syntax Array (Arglist) Parameter Arglist (required) – This parameter the list of values that you want to make...

2 minutes read.

Excel VBA UBound Function

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

1 minute read.

Excel VBA CBool Function

VBA CBool Function: The CBool function in VBA calculates an expression and returns the result as a Boolean data type. Syntax CBool (Expression) Parameter Expression (required) – This parameter represents the expression that that you want...

1 minute read.

Excel VBA DateSerial Function

The DateSerial function in VBA returns a Date from a supplied year, month, and day number. Syntax DateSerial (Year, Month, Day) Parameter Year (required) – This parameter represents an integer signifying the year. Month (required) – This parameter...

2 minutes read.