EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login

Excel VBA MsgBox

Home » VBA » Blog » VBA Message Functions » Excel VBA MsgBox

VBA MsgBox in Excel

Excel VBA MsgBox (Table of Contents)

  • VBA MsgBox in Excel
  • How to Create VBA MSGBox in Excel?

VBA MsgBox in Excel

VBA MsgBox is popup style dialog box on your Excel window which show a specific message. To make the code more interactive, most of the VBA programmers use it in their macro codes

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

VBA Message Box (VBA MsgBox) is an inbuilt dialog box window which displays or shows a message alert box containing an optional set of buttons, icon and other arguments settings

Different parts of the VBA message box

  1. Message box Title: It is typically used to display what the VBA message box is about. If you don’t specify anything, it displays the application name which is “Microsoft Excel” by default in this case.
  2. Prompt or message text: It is the text message or string which you want to convey through the message box to the user. i.e message that you want to display.
  3. Button(s): OK is the default button, where you can customize it to show two or more buttons such as Yes/No, Retry/Ignore, Yes/No/Cancel
  4. Close Icon: It is used to close the VBA message box by clicking on X symbol i.e Close icon.

Syntax for VBA MsgBox Function

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

Or

=MSGBOX(Text_String , [buttons] ,[title], [helpfile, context])

Where,

  • Prompt or Text_String – This is a required parameter or argument. It displays the message that you see in the MsgBox. In our example, the text “This is my first Programme” is the ‘prompt’. You must always enclose the text string in double quotes and limitation is 1,024 characters. This space can be used to write a single or multiple line text or you can even display tables/data.
  • [buttons] – It is an optional argument, which indicates the number and type of buttons which you want to display in the VBA MsgBox. Eg., suppose if you enter “0” as a number or integral value (Button parameter constant) or use vbOkOnly, it will show OK button only in MsgBox
  • [title] – It is an optional argument, this is displayed at the top in the title bar of the VBA MsgBox. If you don’t enter any parameter or left blank in the title argument, then by default it will show an application name i.e. “Microsoft Excel”
  • [helpfile] – It is an optional argument, it’s a string expression which path to help. If you’re using a help file argument in VBA MsgBox syntax, then you need to enter context argument and vice versa
  • [context] – It is an optional argument, it is a numeric expression i.e. Help context number assigned to the appropriate Help topic.

Note: The arguments mentioned in the square brackets are optional. Only the first parameter i.e. ‘prompt’ argument is mandatory & you can ignore the [helpfile] and [context] arguments which are rarely used

Constants for VBA MsgBox

Buttons and icons are a combination of the Buttons argument which is a unique value that drives the output to users to check out button combinations:

VBA MsgBox Styles

20 Button parameter constants

It is used based on the requirement & to make the VBA message box interactive

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

The following applies to the VBA MsgBox Function when the user clicks a button returning a unique value.

Constant

Value

Description

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

How to Create VBA MSGBox in Excel?

Let us now see how to create VBA MsgBox in Excel with the help of an example.

You can download this VBA MsgBox Excel Template here – VBA MsgBox Excel Template

To Create vbOKOnly (Default) VBA message box follow the below steps

  1. Open a VBA Editor window

Select or click on Visual Basic in the Code group on the Developer tab or you can directly click on Alt + F11 shortcut key

Visual Basic

Now, you can see VBA Editor window, under the project window, in VBA project, you can see work file listed (i.e. Sheet1 (VBA MsgBox Example)

VBA Editor Window

  1. Create a module

To create a blank module, right click on Microsoft Excel objects, in that click on Insert and under the menu section select Module, where the blank module gets created

Creating a Module

  1. Creating a VBA message box

Now the blank module is created, it is also called a code window, where you need to type Sub myfirstprogram() as the first message without any quotes around it. Now, you can observe, Excel automatically adds the line End Sub below the first message line when you press Enter.

Line End Sub

Now, all the codes which you enter must be between these two lines, now you can start typing MsgBox function or syntax.

Once you start typing msgbox and leave a space, automatically syntax help feature appears

Syntax Help Feature

Now, I enter only the Prompt argument i.e. “this is my first programme” remaining syntax or argument, I set it as a default

Syntax or Argument

Now, the code is ready,

Sub myfirstprogram()
MsgBox "this is my first programme"
End Sub

you can run the macro by clicking the Run Sub button (i.e. green “play” button) or by pressing F5.

Run Sub button

The VBA message box popup window appears

Popup Window

Similarly, you can create different types of Message Boxes available in Excel VBA i.e. you can add a title, select a different button argument of your choice & multiple lines of text

VBA message box macro code with title & button argument

Sub mysecondprogram()
MsgBox "this is my first programme", vbOKCancel, "WELCOME TO VBA"
End Sub

vba msgbox - Title & Button Argument

Run the code by pressing F5. The VBA message box with title and button popup window appears

VBA MsgBox

Save Macro code

Save your workbook as “Excel macro-enabled workbook”. Press Crl + S, then a popup appears, in that select or click on “No” button

Excel macro-enabled workbook

“Save as” window popup appears, now you can save this file as Excel Macro-Enabled workbook

Save As Window

Once again if you open a file, you can click on shortcut key i.e. ALT +F8, “Macro” dialog box appears, where you can run a saved macro code of your choice

vba msgbox - Macro

Things to Remember About the VBA MsgBox in Excel

  • VBA message box is extremely useful when you are debugging
  • You have an option to insert VBA MsgBox anywhere in your code
  • VBA MsgBox will help you out when you want to ask the user a query.

Recommended Articles

This has been a guide to Excel VBA MsgBox. Here we discuss how to create VBA MsgBox in Excel along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA VLOOKUP
  2. VBA ByVal
  3. VBA Function
  4. VBA XML

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
VBA Message Functions
  • VBA Message
    • Excel VBA MsgBox
    • VBA InputBox
    • VBA List Box
    • VBA TextBox
    • VBA Block Comment
    • VBA Comment
    • VBA Message Box
    • VBA Text
    • VBA Msgbox Yes/No
    • VBA Input
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

Download VBA MsgBox Excel Template

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you