VBA MsgBox Excel Examples - 100+ Message Box Macros

Skip to content Toggle Navigation

120+ Professional Project Management Templates!

Save Up to 85% LIMITED TIME OFFER

View DetailsAnalysistabs Search for: Go to... Home » VBA » VBA MsgBox Excel Examples – 100+ Message Box MacrosNextVBAVBA MsgBox Excel Examples – 100+ Message Box MacrosVBA Concepts|

Share This Post

VBA MsgBox Function In This Article
  • VBA MsgBox Function
    • VBA MsgBox – Syntax:
    • VBA MsgBox in Excel VBA – Example Cases:
    • VBA MsgBox arguments
  • VBA MessageBox Options and Uses
  • Excel VBA MsgBox Yes No Syntax
  • VBA MsgBox Yes No If
  • VBA Message Box New line,carriage return, two lines, multiple line
    • VBA MsgBox Yes No Cancel Return
  • VBA If Then MsgBox and Exit Sub
  • VBA On Error GoTo Message Box for Error Handling
  • VBA MsgBox Styles
  • VBA MsgBox:vbOKOnly
  • VBA MsgBox: vbOKCancel MessageBox
  • VBA MsgBox: vbAbortRetryIgnore
    • VBA MsgBox in Excel: vbYesNoCancel MessageBox
  • VBA MsgBox: vbYesNo
  • MsgBox in Excel VBA: vbRetryCancel MessageBox
  • VBA MsgBox: vbCritical
  • VBA MsgBox: vbQuestion
  • VBA MsgBox: vbExclamation
  • VBA MsgBox: vbInformation
  • VBA MsgBox: vbDefaultButton1
  • VBA MsgBox: vbDefaultButton2
  • VBA MsgBox: vbDefaultButton3
  • VBA MsgBox: vbApplicationModal
  • VBA MsgBox: vbSystemModal
  • VBA MsgBox: vbMsgBoxHelpButton
  • VBA MsgBox: VbMsgBoxSetForeground
  • VBA MsgBox: vbMsgBoxRight
  • VBA MsgBox: vbMsgBoxRtlReading
  • Custom Message Box in Excel VBA:
  • MessageBox Constants in Excel VBA:
    • Message Box Return Constants and Enumerations in Excel VBA:
VBA Code Library
  • VBA Code Explorer
  • Application
    • Methods
      • Calculate
      • CalculateFull
      • FindFile
      • Goto
      • Run
      • Wait
    • Properties
      • Calculation
      • Caption
      • DisplayAlerts
      • DisplayFormulaBar
      • DisplayFullScreen
      • DisplayScrollBars
      • DisplayStatusBar
      • EnableEvents
      • Path
      • ScreenUpdating
      • StatusBar
      • UserName
      • WindowState
  • Workbook
    • Methods
      • Activate
      • Close
      • Protect
      • ProtectSharing
      • RefreshAll
      • RejectAllChanges
      • RemoveUser
      • RunAutoMacros
      • Save
      • SaveAs
      • SaveCopyAs
      • SendMail
      • Unprotect
      • UnProtectSharing
  • Worksheet
    • Methods
      • Activate
      • Calculate
      • Copy
      • Delete
      • Move
      • Protect
      • Select
      • Unprotect
  • Excel VBA Range
    • Methods
      • Activate
      • AddComment
      • AdvancedFilter
      • Calculate
      • Clear
      • ClearComments
      • ClearContents
      • ClearFormats
      • ClearHyperlinks
      • Copy
      • CopyFromRecordset
      • Cut
      • Delete
      • Excel VBA PasteSpecial
      • Find
      • Insert
      • ListNames
      • Merge
  • ActiveX Controls
    • CheckBox
      • Properties
        • Alignment
        • BackColor
        • Caption
        • Enabled
        • Height
        • Left
        • TextAlign
        • Top
        • Width
    • ComboBox
    • CommandButton
    • Image
    • Label
    • ListBox
      • Properties
        • BackColor
        • BorderStyle
        • ColumnCount
        • Enabled
        • Height
        • Left
        • MultiSelect
        • TextAlign
        • Top
        • Width
    • OptionButton
      • Caption
    • TextBox
  • Userform
    • CheckBox
    • ComboBox
    • CommandButton
    • Image
    • Label
    • ListBox
    • OptionButton
    • TextBox
    • Tutorial & Examples
  • Projects
    • Calculator using Excel VBA UserForm
    • Data Entry Userform
    • Excel VBA UserForm: Difference Between Two Dates
    • TOC creator in Excel Workbook using VBA
    • VBA List Folders Subfolders in Directory
    • VBA to Append Data from multiple Excel Worksheets into a Single Sheet – By Column
    • VBA to Consolidate data from multiple Excel Worksheets into a Single Sheet – By Row

REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

View Details

120+ PROFESSIONAL

Project Management Templates

View Details
120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

Share Post

The MsgBox in VBA is a popup message box to display message in Excel VBA, Access VBA and other MS Office Applications. Excel VBA MsgBox shows Message Box using VBA Macro Programming with verity of Options and Types.

Message Box (MsgBox) VBA Macros explained with syntax. Use MsgBox in VBA to show vbYes, No and Cancel, vbexclamation, vbcritical, vbinformation message boxes and other advanced popup messages box models to display with icons and command buttons.

VBA MsgBox Function

VBA MsgBox is one of the most frequently used functions in VBA Application Development. We can use MsgBox Function in Microsoft Word, Excel, Access and PowerPoint VBA Programming. Excel VBA Message Box function displays a message, optional icon and selected set of command buttons in a dialog box. It waits for the user to click a button, and returns an Integer indicating the button which user clicked. Here is the syntax and different kinds of Message Boxes in VBA.

VBA MsgBox – Syntax:

Here is the syntax of VBA MsgBox Function. This is same in Excel, Word, Access, PowerPoint and VBScript.

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

VBA MsgBox in Excel VBA Syntax

Where

  1. Prompt: It Contains String expression displayed as the message in the dialog box. The Maximum length of Prompt is 1024 Characters. You can use carriage return Character,If prompt consists more than one line.
  2. buttons:It Contains Numeric value specifying the number and type of buttons to display.The default button value is 0.
  3. title:It Contains String expression displayed in the title bar of the dialog box.

VBA MsgBox in Excel VBA – Example Cases:

Here is a short video to show you VBA Message Box with different types of options:

https://stg-dotcom-test.kinsta.cloud/wp-content/uploads/2013/08/MsgBox.mp4

Here are the different types of Message Boxes available in Excel VBA. You can click on each link to see the respective examples, Screenshots of output and explanation.

VBA MsgBox arguments

VBA MsgBox will take the following parameters: These options will change the appearance of the Message Box. You can change the model of the Message Box by combining different option of MsgBox Function.

  • MsgBox Prompt: This is the message text which you want to show/prompt
  • MsgBox Buttons Style: This is the type of message box which you want to show, like Yes No buttons with Information Icon
  • MsgBox Title: This is the title of the message box window
  • MsgBox Help File, and Context: These are the other optional parameters which we use in very rare

Here is the Hello World MsgBox Function example with Parameters.

MsgBox “Hello World!”, vbYesNo + vbInformation, “VBA Hello World Message Box Example Title”

The above MsgBox will show you Yes No Type message box with information icon and title.

VBA MessageBox Options and Uses

Let us see the different options and usage of Message Box Function. We can create verity of Message Boxes in VBA to handle different scenarios.

VBA MsgBox Styles

In Most cases we use vbYesNo Message Box and get the result to a variable. Let us see vbYesNo Syntax, arguments, parameters, yes no default buttons, yes no prompt and yes no examples. yes no if syntax helps us to decide based on the user input. We can check If yes no return, yes no answer.

MsgBox “This is the example Yes No Syntax”, vbYesNo

We can also create MsgBox with Yes No and Cancel values, and get the user yes, no or cancel responses. Instead of adding the strings in MsgBox Parameters. We can create variable string and pass as a string. We can use the variable for MsgBox Prompt or Title. Combining Yes No Button Types with different option, we can display yes no critical, yes no warning, yes no exclamation, yes no question type Msg Box. below arr syntax to change button caption, button labels, button names.

Here is the Example with Yes, No, Cancel and Exclamation Icon.

MsgBox “This is the example Yes No Cancel Syntax”, vbYesNoCancel + vbExclamation

We can use Userforms to create customized Message Boxes: MsgBox Without OK button, without buttons, no buttons to show prompt. We can use command buttons, radio buttons in UserForm. We can fortmat the text, Font Size, Font Color and set Bold text in MsgBox.

We can pass variable value or variable text create a string and use as MsgBox variable input for Prompt and Titles. Different buttons and icons of MsgBox are created for different purposes.

We can have multiple lines, access custom buttons, access new line, access carriage return, variable type, variable, error handling, on error goto, error message dialog box, display array, two lines, access multiple lines.

Excel VBA MsgBox Yes No Syntax

The following is the simple Example on VBA MsgBox Yes No Prompt Type. We can use this to receive the acceptance of user to certain criteria. And decide the further process. MsgBox “This is the example Yes No Syntax”, vbYesNo

Check the below example, it will check if user clicked on Yes or No button. We can also show the Help when user pressing F1 button or Help button.

If MsgBox("Do you want to see know the current Time", vbYesNo) = vbYes Then MsgBox Format(Now(), "HH:MM:SS AMPM"), vbInformation, "Current Time" End If

VBA MsgBox Yes No If

The following example on vba msgbox yes no if to show the different messages boxes based on the selected option. If then and exit sub syntax helps terminate the sub procedure based on the certain condition.

Sub sbKnowingUserInput() intUserOption = MsgBox("Press Yes or No Button", vbYesNo) If vbOption = 6 Then MsgBox "You Pressed YES Option" ElseIf vbOption = 7 Then MsgBox "You Pressed NO Option" Else MsgBox "Nothing!" End If End Sub

VBA Message Box New line,carriage return, two lines, multiple line

We can use vbCr to split the message box text into a new line and add carriage return to make into two lines. We can use & vbCr to split the message into multiple lines. MsgBox “Hello, This is Line ONE” & vbCr & “This is Line TWO”

VBA MsgBox Yes No Cancel Return

The below example on vba msgbox yes no cancel return to access the response of MsgBox. This will help us to access,store and input the msgbox response or string in variable value. We can use this variable text in the further programming.

Dim msgValue msgValue = MsgBox("Hello, Are you a graduate? Choos:" _ & vbCr & "Yes: if you are a graduate" _ & vbCr & "Yes: if you are Not a graduate" _ & vbCr & "Yes: if you are Not Intrested" _ , vbYesNoCancel + vbQuestion) If msgValue = vbYes Then MsgBox "You are eligible for applying for this Job" ElseIf msgValue = vbNo Then MsgBox "You are NOT eligible for applying for this Job" ElseIf msgValue = vbCancel Then MsgBox "No Problems, We will find suitable job for you" End If

VBA If Then MsgBox and Exit Sub

Some times we may want to ask user to continue further, other wise skip the execution of next program. The below example on VBA if then msgbox and exit sub will help you to do this:

Sub sbPressYesToExitSub() If MsgBox("Would you like to continue...?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub End If 'The below statements will not be executed when your press Yes button. 'You can write the next programming steps here... This will execute if user selects No in the above prompt. MsgBox "You have not pressed Yes button" End Sub

VBA On Error GoTo Message Box for Error Handling

MsgBox is also useful in error handling. We can tell VBA error message on error. Or we can go to a label and show message box with error number and description. The below code will execute the code and show the error number and description if there is any run-time error.

Sub sbShowing_Error_MessageBox() On Erro GoTo ErrorHanMsg1 'Your code goes here.... Exit Sub 'This comes before End Sub or End Function Statement ErrorHanMsg1: MsgBox Err.Number & vbCr & Err.Description End Sub

VBA MsgBox Styles

Here are the list of styles and models of Message Box Function in VBA. We combine different options to display a message box with desired options.

  • vbOKOnly: Displays the message box with OK button
  • vbOKCancel: This option will show you two buttons, OK and Cancel button to the user.
  • vbAbortRetryIgnore: MsgBox with three buttons, Abort, Retry and Ignore buttons.
  • vbYesNoCancelShows 3 buttons: Yes, No and Cancel.
  • vbYesNo: Shows both Yes, No buttons
  • vbRetryCancel: Helps to display Retry and Cancel buttons
  • vbCritical: Adds Critical Warning Icon to message box
  • vbQuestion: Question mark Icon will be added to message box
  • vbExclamation: Exclamation mark will be added to the MsgBox
  • vbInformation: Information symbol can show on message box
  • vbDefaultButton1: To set the focus on the first button
  • vbDefaultButton2: You can set the focus on the second button
  • vbDefaultButton3: To set the focus on the third button
  • vbDefaultButton4: You can set the focus on the fourth button
  • vbApplicationModal: Close MsgBox to access to Current applications
  • vbSystemModal: Close MsgBox to access to All applications
  • vbMsgBoxHelpButton:Shows Help Button on the message box
  • VbMsgBoxSetForeground:Set MsgBox Foreground
  • vbMsgBoxRight: Text aligned to right.
  • vbMsgBoxRtlReading: RTL support
  • Custom Message Box in Excel VBA: Using UserForms.
  • Message Box Constants in Excel VBA
  • Message Box Return Constants and Enumerations in Excel VBA

VBA MsgBox:vbOKOnly

Please find the following code and output. It will Display OK button only. When we click OK button, It will return value 1 as a output.

Code:
Sub MessageBox_vbOKOnly() 'Variable Declaration Dim OutPut As Integer 'Example of vbOKOnly OutPut = MsgBox("Thanks for visiting Analysistabs!", vbOKOnly, "Example of vbOKOnly") End Sub
Output:

Excel VBA Message Box

Top

VBA MsgBox: vbOKCancel MessageBox

Please find the following code and output. It will Display OK and Cancel buttons. When we click OK button, It will return value 1 as a output.And When we click Cancel button, It will return value 2 as a output.

Code:
Sub MessageBox_vbOKCancel() 'Variable Declaration Dim OutPut As Integer 'Example of vbOKCancel OutPut = MsgBox("You are VBA Expert, is it True?", vbOKCancel, "Example of vbOKCancel") If OutPut = 1 Then 'Output = 1(Ok) MsgBox "Grate! You are VBA Expert, You can learn Advanced Our VBA!", , "Ok - 1" Else 'Output = 2(Cancel) MsgBox "You can Star Learning from Basics!", , "Cancel - 2" End If End Sub
Output:

vbOKCancel Excel VBA Message Box

Top

VBA MsgBox: vbAbortRetryIgnore

Please find the following code and output. It will Display Abort, Retry, and Ignore buttons. When we click Abort button, It will return value 3 as a output. When we click Retry button, It will return value 4 as a output.And When we click Ignore button, It will return value 5 as a output.

Code:
Sub MessageBox_vbAbortRetryIgnore() 'Variable Declaration Dim OutPut As Integer 'Example of vbAbortRetryIgnore OutPut = MsgBox("The Connection has failed. Do you want to Continue?", vbAbortRetryIgnore, "Example of vbAbortRetryIgnore") If OutPut = 3 Then 'Output = 1(Abort) MsgBox "Abort!", , "Abort - 3" ElseIf OutPut = 4 Then 'Output = 4(Retry) MsgBox "Retry!", , "Retry - 4" Else 'Output = 5(Ignore) MsgBox "Ignore!", , "Ignore - 5" End If End Sub
Output:

vbAbortRetryIgnore Excel VBA MsgBox

Top

VBA MsgBox in Excel: vbYesNoCancel MessageBox

Please find the following code and output. It will Display Yes, No, and Cancel buttons. When we click Yes button, It will return value 6 as a output. When we click No button, It will return value 7 as a output.And When we click Cancel button, It will return value 2 as a output.

Code:
Sub MessageBox_vbYesNoCancel() 'Variable Declaration Dim OutPut As Integer 'Example of vbYesNoCancel OutPut = MsgBox("File already exists. Do you want to replace?", vbYesNoCancel, "Example of vbYesNoCancel") If OutPut = 6 Then 'Output = 6(Yes) MsgBox "Yes!", vbInformation, "Yes - 6" ElseIf OutPut = 7 Then 'Output = 7(No) MsgBox "No!", vbInformation, "No - 7" Else 'Output = 2(Cancel) MsgBox "Cancel!", vbInformation, "Cancel - 2" End If End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbYesNo

Please find the following code and output.It will display Display Yes and No buttons. When we click Yes button, It will return value 6 as a output.And, When we click No button, It will return value 7 as a output.

Code:
Sub MessageBox_vbYesNo() 'Variable Declaration Dim OutPut As Integer 'Example of vbYesNo OutPut = MsgBox("Do you want to replace the existing file?", vbYesNo, "Example of vbYesNo") If OutPut = 6 Then 'Output = 6(Yes) MsgBox "Yes! Replace the file", vbInformation, "Yes - 6" Else 'Output = 7(No) MsgBox "No! Don't replace the file", , "No - 7" End If End Sub
Output:

vbYesNo MsgBox VBA

Top

MsgBox in Excel VBA: vbRetryCancel MessageBox

Please find the following code and output. It will Display Retry and Cancel buttons.When we click Retry button, It will return value 4 as a output.And, When we click Cancel button, It will return value 2 as a output.

Code:
Sub MessageBox_vbRetryCancel() 'Variable Declaration Dim OutPut As Integer 'MsgBox VBA Example of vbRetryCancel OutPut = MsgBox("Close the File.Try Again?", vbRetryCancel + vbDefaultButton2, "Example of vbRetryCancel") If OutPut = 4 Then 'Output = 4(Retry) MsgBox "Retry!", , "Retry - 4" Else 'Output = 2(Cancel) MsgBox "Cancel It!", , "Cancel - 2" End If End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbCritical

Please find the following code and output. When we click Ok button, It will return value 1 as a output. And, It will display critical Message Icon.

Code:
Sub MessageBox_vbCritical() 'Variable Declaration Dim OutPut As Integer 'Example of vbCritical OutPut = MsgBox("Please enter valid Number!", vbCritical, "Example of vbCritical") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbQuestion

Please find the following code and output.When we click Ok button, It will return value 1 as a output. And, It will display Warning Query icon.

Code:
Sub MessageBox_vbQuestion() 'Variable Declaration Dim OutPut As Integer 'Example of vbQuestion OutPut = MsgBox("Are you fresher?", vbQuestion, "Example of vbQuestion") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbExclamation

Please find the following code and output.When we click Ok button, It will return value 1 as a output. And, It will display Warning Message icon.

Code:
Sub MessageBox_vbExclamation() 'Variable Declaration Dim OutPut As Integer 'Example of vbExclamation OutPut = MsgBox("Input Data is not valid!", vbExclamation, "Example of vbExclamation") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbInformation

Please find the following code and output.When we click Ok button, It will return value 1 as a output. And, It will display Information Message icon.

Code:
Sub MessageBox_vbInformation() 'Variable Declaration Dim OutPut As Integer 'Example of vbInformation OutPut = MsgBox("Succesessfully Completed the Task.", vbInformation, "Example of vbInformation") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbDefaultButton1

Please find the following code and output. By Default it will focus on first (Retry) Button. When we press enter it will result the value of Retry button as 4.

Code:
Sub MessageBox_vbDefaultButton1() 'Variable Declaration Dim OutPut As Integer 'Example of vbDefaultButton1 OutPut = MsgBox("Close the File.Try Again?", vbRetryCancel + vbDefaultButton1, "Example of vbDefaultButton1") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbDefaultButton2

Please find the following code and output.By Default it will focus on Second(Cancel) Button. When we press enter it will result the value of Retry button as 2.

Code:
Sub MessageBox_vbDefaultButton2() 'Variable Declaration Dim OutPut As Integer 'Example of vbDefaultButton2 OutPut = MsgBox("Close the File.Try Again?", vbRetryCancel + vbDefaultButton2, "Example of vbDefaultButton2") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbDefaultButton3

Please find the following code and output.By Default it will focus on Third(Cancel) Button. When we press enter it will result the value of Retry button as 2.

Code:
Sub MessageBox_vbDefaultButton3() 'Variable Declaration Dim OutPut As Integer 'Example of vbDefaultButton2 OutPut = MsgBox("Close the File.Try Again?", vbYesNoCancel + vbDefaultButton3, "Example of vbDefaultButton3") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbApplicationModal

Please find the following code and output.The user must respond to the message box before continuing work in the current application.

Code:
Sub MessageBox_vbApplicationModal() 'Variable Declaration Dim OutPut As Integer 'Example of vbApplicationModal OutPut = MsgBox("Thanks for visiting Analysistabs!", vbApplicationModal, "Example of vbApplicationModal") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbSystemModal

Please find the following code and output.All applications are suspended until the user responds to the message box.

Code:
Sub MessageBox_vbSystemModal() 'Variable Declaration Dim OutPut As Integer 'Example of vbSystemModal OutPut = MsgBox("Thanks for visiting Analysistabs!", vbSystemModal, "Example of vbSystemModal") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbMsgBoxHelpButton

Please find the following code and output.Adds Help button to the message box.

Code:
Sub MessageBox_vbMsgBoxHelpButton() 'Variable Declaration Dim OutPut As Integer 'Example of vbMsgBoxHelpButton OutPut = MsgBox("Thanks for visiting Analysistabs!", vbMsgBoxHelpButton, "Example of vbMsgBoxHelpButton") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: VbMsgBoxSetForeground

Please find the following code and output.Specifies the message box window as the foreground window.

Code:
Sub MessageBox_VbMsgBoxSetForeground() 'Variable Declaration Dim OutPut As Integer 'Example of VbMsgBoxSetForeground OutPut = MsgBox("Thanks for visiting Analysistabs!", vbMsgBoxSetForeground, "Example of VbMsgBoxSetForeground") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbMsgBoxRight

Please find the following code and output.Here text is right aligned.

Code:
Sub MessageBox_vbMsgBoxRight() 'Variable Declaration Dim OutPut As Integer 'Example of vbMsgBoxRight OutPut = MsgBox("Input Data is not valid!", vbMsgBoxRight, "Example of vbMsgBoxRight") End Sub
Output:

VBA MsgBox in Excel VBA

Top

VBA MsgBox: vbMsgBoxRtlReading

Please find the following code and output.It Specifies text should appear as right-to-left reading on Hebrew and Arabic systems.

Code:
Sub MessageBox_vbMsgBoxRtlReading() 'Variable Declaration Dim OutPut As Integer 'Example of vbMsgBoxRtlReading OutPut = MsgBox("Thanks for visiting Analysistabs!", vbMsgBoxRtlReading, "Example of vbMsgBoxRtlReading") End Sub
Output:

VBA MsgBox in Excel VBA

Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Double click on ThisWorkbook from Project Explorer
  4. Copy the above code and Paste in the code window
  5. Press F5
  6. You should see the above output

Custom Message Box in Excel VBA:

What if your requirement is not achievable with the available types of MessageBox. You Can create your own MessageBox using Forms in Excel VBA. You can design your own custom MessageBox using Form Controls. Here is the example Custom MessageBox. Custom Msgbox in Excel VBA

Top

MessageBox Constants in Excel VBA:

Please find the following table for button argument values:

Constant Value Description
vbOKOnly 0 It Display’s OK button only.
vbOKCancel 1 It Display’s OK and Cancel buttons.
vbAbortRetryIgnore 2 It Display’s Abort, Retry, and Ignore buttons.
vbYesNoCancel 3 It Display’s Yes, No, and Cancel buttons.
vbYesNo 4 It Display’s Yes and No buttons.
vbRetryCancel 5 It Display’s Retry and Cancel buttons.
vbCritical 16 It Display’s Critical Message icon.
vbQuestion 32 It Display’s Warning Query icon.
vbExclamation 48 It Display’s Warning Message icon.
vbInformation 64 It Display’s Information Message icon.
vbDefaultButton1 0 Here first button is default.
vbDefaultButton2 256 Here second button is default.
vbDefaultButton3 512 Here third button is default.
vbDefaultButton4 768 Here fourth button is default.
vbApplicationModal 0 Application modal. The user must respond to the message box before continuing work in the current application.
vbSystemModal 4096 System modal. In this case all applications are suspended until the user responds to the message box.
vbMsgBoxHelpButton 16384 Adds Help button to the message box.
VbMsgBoxSetForeground 65536 Specifies the message box window as the foreground window.
vbMsgBoxRight 524288 Text is right aligned.
vbMsgBoxRtlReading 1048576 Specifies text should appear as right-to-left reading on Hebrew and Arabic systems.

Top

Message Box Return Constants and Enumerations in Excel VBA:

Constant Value Description
vbOK 1 OK
vbCancel 2 Cancel
vbAbort 3 Abort
vbRetry 4 Retry
vbIgnore 5 Ignore
vbYes 6 Yes
vbNo 7 No

Top

Recommended Resource

  • VBA Open File DialogBox Macros
  • VBA InputBox Macros
  • VBA ComboBox
  • VBA ListBox
  • VBA CheckBox
  • VBA CommandButton
  • VBA TextBox
  • VBA Userform CheckBox
  • VBA Userform ComboBox
  • VBA Userform CommandButton
  • VBA Userform Image
  • VBA Userform Label
  • VBA Userform ListBox
  • VBA Userform OptionButton
  • VBA Userform TextBox
Effortlessly Manage Your Projects and Resources120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Browse All TemplatesProject Management Templates

All-in-One Pack 120+ Project Management Templates

120+ PM Templates

Essential Pack 50+ PM Templates

50+ PM Templates

Excel Pack 50+ Excel PM Templates

50+ Excel Templates

PowerPoint Pack 50+ Excel PM Templates

50+ PPT Templates

MS Word Pack 25+ Word PM Templates

25+ Word Templates
Ultimate Project Management Template
Ultimate PM Template
Ultimate Resource Management Template
Resource Management
Project Portfolio Management Templates
PPM Templates

Related Posts

VBA AutoFilter Method VBA AutoFilter Method Gallery

VBA AutoFilter Method

March 29, 2018|1 Comment Hide Developer Tab in Excel Ribbon Hide Developer Tab in Excel Ribbon Gallery

Hide Developer Tab in Excel Ribbon

October 8, 2016|0 Comments Show or Hide|Unload a userform Show or Hide|Unload a userform Gallery

Show or Hide|Unload a userform

October 2, 2015|1 CommentCategories: VBATags: VBA ConceptsLast Updated: September 24, 2023

19 Comments

  1. Shady Mohsen January 21, 2014 at 11:02 PM - Reply

    Thanks friend. It helped me a lot.I appreciate your efforts on creating useful VBA codes.

  2. ramana January 31, 2015 at 11:57 PM - Reply

    nice post.. is there any suggestion how to display message box from the statement ‘For – Next’ , but the message itself does not appear repeatedly based on that ‘For-Next’ values?

  3. PNRao February 3, 2015 at 10:14 PM - Reply

    Hi Ramana, You can use a Boolean variable to do this:

    Sub ShowMsgOnceInForLoop() Dim msgFlag As Boolean msgFlag = False For iCntr = 1 To 100 If msgFlag = False Then MsgBox "This is MSGBox" msgFlag = True End If Next End Sub

    Instead of this flag, you may use any other condition when you want to show the Message box.

    Thanks-PNRao!

  4. Dilip March 4, 2015 at 1:46 PM - Reply

    i want to replace MsgBox appearing for Data Validation – Input & Error Message. I want to skip Help Button in Excel Default Message and add our own Message Title. Is there any way to do this ? Pl. provide VBA code only. Don’t waste your time in explaining how this can be done through Ribbon Menu pl. I will be highly obliged if i get the solution asap.If you require further information pl. let me know asap.

  5. PNRao March 7, 2015 at 7:34 PM - Reply

    Hi Dilip,

    Please see the below VBA example code for Data validation and Custom mesagebox.

    Sub sbCustomDatavalidation() With Range("A1:A5").Validation .Delete .Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _ Operator:=xlBetween, Formula1:="1", Formula2:="5" .IgnoreBlank = True .InCellDropdown = True .InputTitle = "Enter #Items" .InputMessage = "Enter an value between 1 to 5" .ErrorTitle = "My Message Box title" .ErrorMessage = "My Message Box Description" .ShowInput = True .ShowError = True End With End Sub

    Thanks-PNRao!

  6. Paul April 14, 2015 at 11:43 PM - Reply

    Hey Valli, Great article!

    I was wondering … I’d like a box to pop up for one second (or other time period), then dismiss itself without user interaction. Can msgbox be made to do this, or is there a different command that could do this? Thanks

  7. pratyush December 26, 2015 at 9:10 PM - Reply

    I learned so many things from all above.Thanks and please stay-Tuned. All the VBA beginners like me are refering all these and its very helpful.

    Thanks Again. .

  8. Csaba March 2, 2016 at 6:01 PM - Reply

    How I can stop the “X” button from the upper right corner to close the msgbox, practically force the user to respond with assigned buttons. Something similar with UserForm_QueryClose(Cancel As Integer, CloseMode As Integer), cancel = false, and post a message.

  9. Stephen Nzai September 6, 2016 at 7:40 PM - Reply

    Can someone tell me how to put the displaced value on a message box on a cell. Lets say the message box displays integer 5, how do I get it on a cell without typing it?

  10. sambit September 15, 2016 at 10:56 AM - Reply

    i need VBA code so that i can get an alert when a cell in excel exceeds certain specified number which is automatically populated by the server

  11. Bob November 8, 2016 at 2:25 AM - Reply

    Funny everyone illustrates how to add a help button, but no one will attempt to demonstrate how to get the help button to display help. The help button example above works great and pops up an empty help file. However if you add the next parameter, the help file path, vbscript complains – “Invalid procedure call or arguments: MsgBox”. The “.chm” file I tested with works great if you click on the file directly. Does this mean that not all .chm help files are windows compatible or is MsgBox broken.

  12. ParismaX February 14, 2017 at 6:03 PM - Reply

    I was wondering if I could make a message box display the user’s name. I know it is possible to do this but how would I go about it?

  13. Gregory Feeney May 27, 2017 at 8:29 PM - Reply

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) Dim MyValue As String ‘Set MyValue to whatever you want MyValue = 1

    ‘Set the Range to what ever cell you want to monitor changes

    If Range(“A1”) > MyValue Then MsgBox “Alert Box Appears” End If

    End Sub

  14. Nabil Mourad June 4, 2017 at 5:56 PM - Reply

    Sub DisplayUserName() msgBox “The User Name is: ” & Environ(“UserName”),vbInformation,”User Name” End Sub

  15. Colin Riddington June 17, 2017 at 3:30 AM - Reply

    You can use the Environ function to get the logged on user name

    e.g. MsgBox “Hello ” & Environ(“UserName”),vbExclamation,”MsgBox Title”

    However the user name may not give the person’s forename. Otherwise use DLookup to find the forename in a table. e.g. If you have a table tblUsers with logged user info including a field called Forename and UserID stored as a string strUserID, you could use DLookup something like this:

    MsgBox “Hello ” & DLookup(“Forename”,”tblUsers”,”UserID”= ‘” & strUserID & “‘”),vbExclamation,”MsgBox Title”

  16. Ariful Romadhon August 5, 2017 at 3:54 PM - Reply

    Could you help me please? I want to make message box for validating surveys. the message box contain the message because error of stuffing

    I want my message box keep showing, so i can click the sheets which contain error of stuffing without closing the message box.

    So, the message box will guide me to fix the error in that sheets

    This is my previous code:

    Dim error As String error = ”

    If (vehicle = True) And (gasoline_month = 0) Then error = error & “- the expenditure of gasoline should not be empty” & Chr(10) End If

    If error = “” Then msgbox “clean”, vbInformation Else MsgBox error, vbCritical End Sub

    Thank you, I hope anyone can help me,, (sorry for my bad english)

  17. rathy August 12, 2017 at 10:23 PM - Reply

    Dim msgValue msgValue = MsgBox(“Hello, Are you a graduate? Choos:” _ & vbCr & “Yes: if you are a graduate” _ & vbCr & “Yes: if you are Not a graduate” _ & vbCr & “Yes: if you are Not Intrested” _ , vbYesNoCancel + vbQuestion)

    I think the above incorrect right, it should be

    Dim msgValue msgValue = MsgBox(“Hello, Are you a graduate? Choos:” _ & vbCr & “Yes: if you are a graduate” _ & vbCr & “No: if you are Not a graduate” _ & vbCr & “Cancel: if you are Not Intrested” _ , vbYesNoCancel + vbQuestion)

    .

  18. Mike September 4, 2017 at 7:26 PM - Reply

    Very helpful. Perfect Macros. Thanks you.

  19. ajay October 10, 2020 at 8:20 PM - Reply

    can give msg box button a person name.. just like yes no or ok cancel

    thanks

Leave A Comment Cancel reply

Comment

Save my name, email, and website in this browser for the next time I comment.

Effectively Manage Your Projects and  Resources

With Our Professional and Premium Project Management Templates!View Details

ANALYSISTABS.COM provides free and premium project management tools, templates and dashboards for effectively managing the projects and analyzing the data.

We’re a crew of professionals expertise in Excel VBA, Business Analysis, Project Management. We’re Sharing our map to Project success with innovative tools, templates, tutorials and tips.

Project Management

Excel VBA

Download Free Excel 2007, 2010, 2013, 2016, 2019 + Microsoft Office 365 Templates, Dashboards, Tools for Project Management, Data Mining, Analysis, Visualization and VBA Automation. Learn Project Management and VBA for Microsoft Excel, Word, PowerPoint, Access, Outlook to develop applications for retail, insurance, banking, finance, telecom, healthcare domains.

Analysistabs Logo

© 2023 Analysistabs | Sitemap | Your Privacy | Terms

Page load link Go to Top

Từ khóa » Visual Basic Msgbox Example