EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Message Functions VBA Message Box
Secondary 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

VBA Message Box

By Pallavit SharmaPallavit Sharma

VBA Message Box

Excel VBA Message Box

There are two things in this word: VBA and the other is Message Box. In this, I’ll be explaining how to use the Message Box function using VBA (Visual Basic for Applications).

VBA – It’s a programming language for those who work in Excel and other Office programs to automate tasks in Excel by writing so-called Macros.

Watch our Demo Courses and Videos

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

Message Box – As the name suggests, it is nothing but a dialog box used to notify the user about something or give some information, showing a custom message with some custom buttons like Yes/No or Cancel/Ok.

output of message box

Message Box has various parts:

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,142 ratings)

Title – It will display the Title of the Message box, i.e. what the message box is about. By default, it will show the name of the application “Microsoft Excel” because we are opening it in Excel.

Prompt – It shows the message that we want to display in the dialog box.

Button – We can add and customize different buttons in the message box. Buttons we can add like Cancel, Ignore, Abort, Ok, Retry, etc. OK is a default button which is displayed.

Close Icon – It has a red color close icon from where we can directly switch off the Message Box.

As we know, MsgBox is a VBA function like other functions; it also has syntax and arguments to be passed in it.

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

Syntax of VBA Message Box in Excel

The syntax for VBA Message Box is as follows:

Syntax of VBA Message Box

Prompt – As we have discussed earlier, that Prompt is a required argument. It displays the message that we see in the MsgBox. Our example text is “ This is Message Box Demo…” is the Prompt. We can use 1024 characters in a prompt and can also be used to display the values of the variables.

Buttons – It displays the buttons in the box. Suppose if I use vbOKCancel, it will display both the OK and Cancel buttons,  and if I use vbOKOnly, it will display only the OK button.

Helpfile – It opens the help file when the user clicks on the Help button. The Help button will only appear if we write the code for it. We also need to specify the context arguments while using the help file.

NOTE – Only the “Prompt” argument is mandatory; all other arguments are Optional.

Button Constants in MsgBox

In this section, we are going to cover the different buttons used in the Message Box:-

  • vbOKOnly – This only shows the OK button.
  • vbOKCancel – This shows the OK and Cancel buttons.
  • vbYesNo –  This shows Yes/ No buttons.
  • vbYesNoCancel –  This shows Yes, No, and Cancel buttons.
  • vbAbortRetryIgnore – This shows Abort, Retry and Ignore Buttons.
  • vbRetryCancel – This shows Retry and Cancel buttons.
  • vbMsgBoxHelpButton – This shows the Help button.

Now we will see how the Message Box is created in the VBA.

To enable Macro’s in Excel, you have to first enable the Developers Tab and then change the settings through which Macro’s run.

Creating Message Box VBA Code in Excel

Below are the different examples of Message Box VBA code in Excel. Let’s understand this with the help of an example.

You can download this VBA Message Box Excel Template here – VBA Message Box Excel Template

Example #1

Step 1: Open the Developer tab, select the Visual Basic Editor to form the Code group, or you can also press F11 to open it.

VBA Message Box Example 1-1

Step 2: After pressing F11, Microsoft VBA (Visual Basic for Applications) windows appears.

Code:

Sub Messagebox()

End Sub

VBA Message Box Example 1.2

From above, we can see that the Macro code which we are writing will work only with the sheet we are working with, i.e. “VBA msgbox.xlsm.”

Step 3: Now, we will type the code for MsgBox to display “This is Message box Demo…!”. The code for it is.

Code:

Sub Messagebox()

msgbox "This is a Message Box Demo...! "

End Sub

Example 1.3

Step 4: Now, we will run the following code by clicking on RUN or pressing on F5. As we can see, that Message Box is displayed with the OK button and Message.

 OK button and Message

Example #2

In this, we are going to display the Message with Icon constants, Yes, No, and Title. All the steps from 1-3 are the same; we have to make changes in step 4.

Step 4:

Code:

Sub Messagebox()

msgbox "This file contains virus. Do you want to continue", vbYesNo + vbExclamation, "This is Title"

End Sub

Example 1.4

Step 5: As we can see, that message box is displayed with the with Icon constants (Exclamation mark), Yes, No, and Title.

Icon constants (Exclamation mark)

Things to Remember

  1. A message Box is used to display a message or warning on the screen, similar to a dialog box.
  2. This “Prompt” argument is mandatory; all other arguments are Optional.
  3. OK button is the default button.
  4. VBA code is halted when the Message Box is displayed.

Recommended Articles

This is a guide to VBA Message Box. Here we discuss how to get Message Box in VBA Excel, practical examples, and a downloadable excel template. You can also go through our other suggested articles –

  1. VBA UBound
  2. VBA Get Cell Value
  3. VBA END
  4. VBA RGB
Popular Course in this category
MS Excel Training Bundle
  13 Online Courses |  100+ Hours |  Verifiable Certificates |  Lifetime Validity
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • 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

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - 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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more