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 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.
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
https://analysistabs.com/vba/msgbox/ 2/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
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 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.
https://analysistabs.com/vba/msgbox/ 3/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
The above MsgBox will show you Yes No Type message box with
information icon and title.
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.
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.
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
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.
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
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
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”
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)
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
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.
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
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.
https://analysistabs.com/vba/msgbox/ 7/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
VBA MsgBox:vbOKOnly
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
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
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
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
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
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
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
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
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
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
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
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
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
Code:
Sub MessageBox_vbDefaultButton3()
'Variable Declaration
https://analysistabs.com/vba/msgbox/ 17/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
'Example of vbDefaultButton2
OutPut = MsgBox("Close the File.Try Again?", vbYesNoCancel + vbDefaultButton3, "Example of vbDefaultButton3")
End Sub
Output:
Top
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
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
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
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
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
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:
Top
https://analysistabs.com/vba/msgbox/ 22/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
https://analysistabs.com/vba/msgbox/ 23/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
Top
vbOK 1 OK
vbCancel 2 Cancel
vbAbort 3 Abort
vbRetry 4 Retry
vbIgnore 5 Ignore
vbYes 6 Yes
vbNo 7 No
Top
Recommended Resource
https://analysistabs.com/vba/msgbox/ 24/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
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 2010
Microsoft
Excel 2013
Microsoft
Excel 2016
https://analysistabs.com/vba/msgbox/ 26/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
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
https://analysistabs.com/vba/msgbox/ 27/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
Related Posts
18 Comments
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?
Hi Ramana,
You can use a Boolean variable to do this:
Sub ShowMsgOnceInForLoop()
Dim msgFlag As Boolean
msgFlag = False
Next
End Sub
https://analysistabs.com/vba/msgbox/ 28/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
Thanks-PNRao!
Hi Dilip,
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"
Thanks-PNRao!
https://analysistabs.com/vba/msgbox/ 29/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
Thanks Again. .
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.
https://analysistabs.com/vba/msgbox/ 30/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
End Sub
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
https://analysistabs.com/vba/msgbox/ 32/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
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)
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)
Leave A Comment
https://analysistabs.com/vba/msgbox/ 33/34
02/05/2018 VBA MsgBox Excel Examples - 100+ Message Box Macros
Comment...
POST COMMENT
ABOUT US SITEMAP
https://analysistabs.com/vba/msgbox/ 34/34