Anda di halaman 1dari 34

02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Home About Us BLOG VBA FORUM Contact Us My Account      

VBA MsgBox Excel Examples – 100+ Message Box Macros


Home / VBA / VBA MsgBox Excel Examples – 100+ Message Box Macros

 Previous Next  Download Free Excel 2007, 2010, 2013


Add-in for Creating Innovative
Dashboards, Tools for Data Mining,
Analysis, Visualization. Learn VBA for MS
Excel, Word, PowerPoint, Access, Outlook
to develop applications for retail,
Best Excel VBA MsgBox Examples to display Message Box using VBA insurance, banking, nance, telecom,
healthcare domains.
with Options and Types. Message Box (msgbox) VBA Macros
explained with syntax. Use MsgBox in VBA to show vbYes, No and  100+ Most useful VBA
Cancel, vbexclamation, vbcritical, vbinformation message boxes and Examples
 100+ VBA Interview
other advanced popup messages box models to display with icons Questions: Explained
 15 Macros for Absolute
and command buttons.
Beginners
 15 Must Known Excel Tips

50+ PROJECT ADVANCED


PREMIUM MANAGEMENT PROJECT
TEMPLATES PLAN &
TEMPLATES PACK PORTFOLIO

LIMITED TIME Excel


PowerPoint
TEMPLATE
Excel Template
OFFER Word

VIEW

ON VIEW
BUSINESS
DETAILS
20+ EXCEL
DETAILS

PRESENTATIONSPROJECT
SALE 80% TEMPLATES MANAGEMENT
OFF PACK PACK
PowerPoint Slides Excel
Templates

VIEW
DETAILS VIEW
DETAILS Knowledge Base
20+ 10+ MS
POWERPOINT WORD
 Data Analysis
PROJECT PROJECT
MANAGEMENT PACK
MANAGEMENT
 Excel
PowerPoint PACK
Templates
https://analysistabs.com/vba/msgbox/ 1/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Word
VIEW  Excel Add-ins
DETAILS Templates

 Excel Dashboards
VIEW
BROWSE ALL DETAILS  Excel Formulas
TEMPLATES
 Excel Templates

 Excel VBA

 Pivot Tables

 SQL

VBA MsgBox Function  VBA

 VBA Filter
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 di erent kinds of Message Boxes in
VBA.

VBA MsgBox – Syntax:

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

MsgBox(prompt
FREE VBA CODE

EXPLORER
[, buttons] [, title] [, help le, context])
ASK EXCEL VBA

QUESTION

Where

1. Prompt: It Contains String expression displayed as the


message in the dialog box. The Maximum length of Prompt is

https://analysistabs.com/vba/msgbox/ 2/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

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 di erent
types of options:

00:00 02:48

Here are the di erent 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 di erent 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

https://analysistabs.com/vba/msgbox/ 3/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

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 di erent options and usage of Message Box Function.
We can create verity of Message Boxes in VBA to handle di erent
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 stings 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 di erent 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.

https://analysistabs.com/vba/msgbox/ 4/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

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. Di erent buttons and
icons of MsgBox are created for di erent 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 di erent


messages boxes based on the selected option. If then and exit sub
syntax helps terminate the sub procedure based on the certain
condition.

https://analysistabs.com/vba/msgbox/ 5/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

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 nd 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

https://analysistabs.com/vba/msgbox/ 6/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

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 di erent 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.

https://analysistabs.com/vba/msgbox/ 7/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

vbYesNo: Shows both Yes, No buttons


vbRetry
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 rst 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 nd 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:

https://analysistabs.com/vba/msgbox/ 8/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Top

VBA MsgBox: vbOKCancel MessageBox

Please nd 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:

https://analysistabs.com/vba/msgbox/ 9/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Top

VBA MsgBox: vbAbortRetryIgnore

Please nd 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:

Top

VBA MsgBox in Excel: vbYesNoCancel


MessageBox
Home  VBA Code Explorer  BLOG  PROJECT MANAGEMENT TEMPLATES     BUY NOW 
https://analysistabs.com/vba/msgbox/ 10/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
p  J 
Please nd 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:

Top

VBA MsgBox: vbYesNo

Please nd 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.

https://analysistabs.com/vba/msgbox/ 11/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
Code:

Sub MessageBox_vbYesNo()

'Variable Declaration
Dim OutPut As Integer

'Example of vbYesNo
OutPut = MsgBox("Do you want to replace the existing le?", vbYesNo, "Example of vbYesNo")

If OutPut = 6 Then
'Output = 6(Yes)
MsgBox "Yes! Replace the le", vbInformation, "Yes - 6"
Else
'Output = 7(No)
MsgBox "No! Don't replace the le", , "No - 7"
End If

End Sub

Output:

Top

MsgBox in Excel VBA: vbRetry


vbRetryCancel
MessageBox

Please nd 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()
vbRetry

'Variable Declaration
Dim OutPut As Integer

https://analysistabs.com/vba/msgbox/ 12/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

'MsgBox VBA Example of vbRetry


vbRetryCancel
OutPut = MsgBox("Close the File.Try Again?", vbRetry
vbRetryCancel + vbDefaultButton2, "Example of vbRetry
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:

Top

VBA MsgBox: vbCritical

Please nd 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:

https://analysistabs.com/vba/msgbox/ 13/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Top

VBA MsgBox: vbQuestion

Please nd 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:

Top

VBA MsgBox: vbExclamation

Please nd the following code and output.When we click Ok button, It


will return value 1 as a output. And, It will display Warning Message

https://analysistabs.com/vba/msgbox/ 14/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

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:

Top

VBA MsgBox: vbInformation

Please nd 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

https://analysistabs.com/vba/msgbox/ 15/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
Output:

Top

VBA MsgBox: vbDefaultButton1

Please nd the following code and output. By Default it will focus on


rst (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?", vbRetry
vbRetryCancel + vbDefaultButton1, "Example of vbDefaultButton1")

End Sub

Output:

Top

https://analysistabs.com/vba/msgbox/ 16/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

VBA MsgBox: vbDefaultButton2

Please nd 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?", vbRetry
vbRetryCancel + vbDefaultButton2, "Example of vbDefaultButton2")

End Sub

Output:

Top

VBA MsgBox: vbDefaultButton3

Please nd 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

https://analysistabs.com/vba/msgbox/ 17/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Dim OutPut As Integer

'Example of vbDefaultButton2
OutPut = MsgBox("Close the File.Try Again?", vbYesNoCancel + vbDefaultButton3, "Example of vbDefaultButton3")

End Sub

Output:

Top

VBA MsgBox: vbApplicationModal

Please nd 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:

https://analysistabs.com/vba/msgbox/ 18/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Top

VBA MsgBox: vbSystemModal

Please nd 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:

Top

VBA MsgBox: vbMsgBoxHelpButton

Please nd 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")

https://analysistabs.com/vba/msgbox/ 19/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

End Sub

Output:

Top

VBA MsgBox: VbMsgBoxSetForeground

Please nd the following code and output.Speci es 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:

Top

VBA MsgBox: vbMsgBoxRight


https://analysistabs.com/vba/msgbox/ 20/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Please nd 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:

Top

VBA MsgBox: vbMsgBoxRtlReading

Please nd the following code and output.It Speci es 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:

https://analysistabs.com/vba/msgbox/ 21/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

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.

Top

MessageBox Constants in Excel VBA:

Please nd the following table for button argument values:

https://analysistabs.com/vba/msgbox/ 22/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Constant Value Description

vbOKOnly 0 It Display’s OK button only.

It Display’s OK and Cancel


vbOKCancel 1
buttons.

It Display’s Abort, Retry, and


vbAbortRetryIgnore 2
Ignore buttons.

It Display’s Yes, No, and Cancel


vbYesNoCancel 3
buttons.

vbYesNo 4 It Display’s Yes and No buttons.

It Display’s Retry and Cancel


vbRetry
vbRetryCancel 5
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.

It Display’s Information Message


vbInformation 64
icon.

vbDefaultButton1 0 Here rst button is default.

vbDefaultButton2 256 Here second button is default.

vbDefaultButton3 512 Here third button is default.

vbDefaultButton4 768 Here fourth button is default.

Application modal. The user must


respond to the message box
vbApplicationModal 0
before continuing work in the
current application.

System modal. In this case all


applications are suspended until
vbSystemModal 4096
the user responds to the message
box.

Adds Help button to the message


vbMsgBoxHelpButton 16384
box.

Speci es the message box


VbMsgBoxSetForeground 65536 window as the foreground
window.

vbMsgBoxRight 524288 Text is right aligned.

vbMsgBoxRtlReading 1048576 Speci es text should appear as


right-to-left reading on Hebrew

https://analysistabs.com/vba/msgbox/ 23/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

and Arabic systems.

Top

MessageBox 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

https://analysistabs.com/vba/msgbox/ 24/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

LIMITED TIME OFFER - GET IT


NOW!
ADVANCED PROJECT PLAN EXCEL
TEMPLATE

LIMITED TIME OFFER - GET IT


NOW!
BUSINESS POWERPOINT
PRESENTATIONS TEMPLATES
PACK

 
 

Related Resource

Excel VBA
Project Management Reference
Reference

https://analysistabs.com/vba/msgbox/ 25/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Excel VBA
Project Management Reference
Reference

VBA Reference:

VBA Code
Explorer
VBA Excel
Application
VBA Excel
Workbook What is a Project?
VBA Excel Project Appraisal
Worksheet Project Management
VBA Excel Project Plan
Range Project Resource
VBA ActiveX What is Gantt Chart?
Controls Excel Templates
VBA Excel Project Management
Userforms Templates
VBA Projects PowerPoint Project
Management Templates
Excel Reference:
MS Word Project Management
Excel Tutorial Templates
Microsoft
Excel 2003 Excel Basics Tutorials

Microsoft Excel VBA Tutorials

Excel 2007 Excel Functions and Formulas

Microsoft
Excel 2010
Microsoft
Excel 2013
Microsoft
Excel 2016

External VBA Reference

Excel VBA Reference Project Management Reference

https://analysistabs.com/vba/msgbox/ 26/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Excel VBA Reference Project Management Reference

Project Management
Methodologies
VBA Reference:
MS Project Templates
Excel, Word, PowerPoint
Timeline Templates
Getting Started with VBA
Excel, Word, PowerPoint
in O ce
Planners and Trackers
VBA for MS O ce
Microsoft Excel Templates
Application: Complete
MS PowerPoint Excel
References
Templates
Excel VBA Reference
MS Word Templates
VBA Tutorials
MS O ce Schedules
VBA Macros
Templates
VBA Functions
Excel, Word, PowerPoint
Excel Reference: Agenda Templates
Excel, Word, PowerPoint
Microsoft Excel
Invoices Templates
Reference
Excel, Word, PowerPoint
Excel Formulas
Business Plans Templates
Excel 2016
Excel, Word, PowerPoint
Excel Functions
Financial Management
Excel Quick Tutorials
Templates
Excel, Word, PowerPoint
Minutes Templates

By Valli | August 25th, 2013 | VBA | 18 Comments

Share This Story,     


Choose Your Platform!    

About the Author: Valli

Excel VBA Developer having around 8 years of experience in using


Excel and VBA for automating the daily tasks, reports generation and
dashboards preparation. Valli is sharing useful VBA examples ad Tips
to helps us automating daily tasks.

https://analysistabs.com/vba/msgbox/ 27/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Related Posts

18 Comments

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

Thanks friend. It helped me a lot.I appreciate your e orts


on creating useful VBA codes.

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?

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

https://analysistabs.com/vba/msgbox/ 28/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Instead of this ag, you may use any other


condition when you want to show the Message
box.

Thanks-PNRao!

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.

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!

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

https://analysistabs.com/vba/msgbox/ 29/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

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 di erent
command that could do this?
Thanks

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. .

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.

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?

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 speci ed number which is
automatically populated by the server

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

https://analysistabs.com/vba/msgbox/ 30/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

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

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 le. However if you
add the next parameter, the help le path, vbscript
complains – “Invalid procedure call or arguments: MsgBox”.
The “.chm” le I tested with works great if you click on the
le directly. Does this mean that not all .chm help les are
windows compatible or is MsgBox broken.

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?

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

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

https://analysistabs.com/vba/msgbox/ 31/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
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 nd the forename in a
table.
e.g. If you have a table tblUsers with logged user info
including a eld 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”

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
stu ng

I want my message box keep showing, so i can click the


sheets which contain error of stu ng without closing the
message box.

So, the message box will guide me to x 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

https://analysistabs.com/vba/msgbox/ 32/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

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)

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)

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

Very helpful. Perfect Macros. Thanks you.

Leave A Comment

https://analysistabs.com/vba/msgbox/ 33/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros

Comment...

Name (required) Email (required) Website

POST COMMENT

ABOUT US SITEMAP

ANALYSISTABS.COM provides  Sitemap


tools and add-ins for analyzing the
data and building innovative  Terms and Conditions
dashboards. Our aim is to create
innovative tools and dashboards  Privacy Policy
for analyzing the data. And make
this place as the best source for all
business analysts to learn & share
data analysis concepts and its
relevant tools for creating
innovative dashboards &
awesome tools for analyzing the
data.

© Copyright 2012-2023 ANALYSISTABS | All Rights Reserved.      

https://analysistabs.com/vba/msgbox/ 34/34

Anda mungkin juga menyukai