Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Excel Tutorial
  • Excel Formulas
  • Excel Shortcut Keys
  • Data Analysis in Excel
  • Formatting in Excel
  • Excel Workbooks
  • Statistical Functions
  • Data Visualization in Excel
  • Pivot Tables in Excel
  • MS Excel Quiz
  • Excel Interview Questions
  • Advance Excel
Open In App
Next Article:
MessageBox in Excel VBA
Next article icon

MessageBox in Excel VBA

Last Updated : 22 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Message Box is one of the most frequently used functions in VBA Macro. The message box is a dialogue box that appears on the screen as an extra window. The dialogue box that appeared asks input from the user and returns a constant/code/number accordingly or it can use to display messages. In this article, we will learn how to create different types of message boxes in VBA Macro. 

Message Box 

A message box is a dialogue box that displays information that can be text, date, number, etc. like as shown in the below image. In Excel, we can easily create a message box using VBA.

Message-box

Parts of a message box: 

1. Title: The title of a message box specifies what properties or functions will this dialogue box performs. While writing a message box in VBA macro it is not mandatory to write a title. A title does not have multiple lines. If a title is not mentioned then by default the title becomes Microsoft Excel. 

Title-of-message-box

2. Prompt: The prompt is the heart of the message box. It specifies the work to be done or the information provided in a detailed manner. While writing a message box function it is mandatory to write a prompt statement. You can also display tables, dates, and numbers in the prompt. You can also add multiple lines in the prompt area. 

Prompt-text

3. Buttons: While working with basic Excel, you might have configured the different buttons that appear in a dialogue box. For example, Yes/No, Retry, Help, Abort/Retry/Ignore, etc. You can create and use any of the buttons, according to your requirement. While writing a message box function it is not mandatory to specify the buttons. By default, Ok appears. 

Buttons-in-message-box

Creating a Basic Message Box in VBA

In Excel, we can easily create a message box with the help of the MsgBox function. 

Syntax:

Msgbox "prompt(mandatory)", buttons(temporary), "title(temporary)", "helpfile(temporary)", "context(temporary)"

Here, 

  • Prompt: Prompt is the detailed information of the MsgBox. It is mandatory to add a prompt while writing the MsgBox function. You can also add multiple lines to the prompt.
  • Buttons: It is the temporary argument that you wish to add. By default, it shows only the Ok button. To add custom buttons, you have the code for each button. For example, vbRetryCancel, vbOkOnly.
  • Title: It is the temporary argument of the MsgBox function. You cannot add multiple titles to a message box.
  • HelpFile: This is a very rare argument that is passed on while creating a MsgBox. This argument is only provided when you are using a help button. When you are passing the help file, you have to use context arguments also.
  • Context: It is only used with the help file and help button. You need to have a numeric help context number to the appropriate help topic.

Follow the below steps:

Step 1: The MsgBox keyword is used to create a message box is MsgBox. So write MsgBox. Start with a double quote and write the prompt you want to display. Press Enter. 

Creating-message-box

Step 2: Click on the Run button. 

Click-on-run-button

Step 3: The Message Box appears. Here, the VBA code has stopped running and will not get executed until we select any of the buttons in the message box. Click Ok. 

 Message-Box-appears

Step 4: The VBA code resumes. 

VBA-code-resumes

Buttons in MessageBox

When you have typed a prompt in the MsgBox function. The next argument is separated with a comma(,). Now, the VBA editor gives us a list of buttons that can be added to the MsgBox function. You can select and add the button according to your requirements.  

Lists-of-buttons

The types of buttons in the MsgBox.

1. vbOkOnly(Default): This is the default button that appears. To create this button follow the following steps: 

Step 1: Write the code. vbOkOnly is the code that helps create only the Ok button. 

Write-the-following-code

Step 2: The MsgBox contains only the Ok button. 

This message-box-contains-only-one-ok-button

2. vbOkCancel: vbOkCancel code tells the VBA compiler to show Ok and cancel buttons while displaying the MsgBox. To create this button follow the following steps: 

Step 1: Write the code. vbOkCancel is the code that helps create the Ok and Cancel buttons. 

Write-the-code

Step 2: The MsgBox contains both Ok and Cancel.  

Message-box-contains-two-buttons

3. vbAbortRetryIgnore: This code displays Abort, Retry and Ignore buttons in the message box. To create this button follow the following steps: 

Step 1: Write the code. vbAbortRetryIgnore is the code that helps create the Abort, Retry, and Ignore buttons. 

Creating-vbAbortRetryIgnore

Step 2: The MsgBox contains all three Abort, Retry and Ignore.

vbAbortRetryIgnore-is-created

4. vbYesNoCancel: vbYesNoCancel code displays Yes, No, and Cancel buttons in the message box. To create this button follow the following steps: 

Step 1: Write the code. vbYesNoCancel is the code that helps create the Yes, No, and Cancel.

Creating-vbYesNoCancel-buttons

Step 2: The MsgBox contains all three Yes, No, and Cancel.

vbYesNoCancel-is-created

5. vbYesNo: vbYesNo code displays Yes and No buttons in the message box. To create this button follow the following steps: 

Step 1: Write the code. vbYesNo is the code that helps create the Yes and No.

Creating-vbYesNo-buttons

Step 2: The MsgBox contains both Yes and No.

vbYesNo-is-created

6. vbRetryCancel: vbRetryCancel code displays Retry and Cancel buttons in the message box. To create this button follow the following steps: 

Step 1: Write the code. vbRetryCancel is the code that helps create the Retry and Cancel.

Creating-vbRetryCancel-button

Step 2: The MsgBox contains both Retry and Cancel.

vbRetryCancel-is-created

7. vbDefaultButton2: You can use any of the default buttons. There are four types of default buttons. vbDefaultButton1, vbDefaultButton2, vbDefaultButton3, vbDefaultButton4. Here, vbDefaultButton2 is the button that is selected by default when a Msgbox appears. If you press Enter, then it automatically gets selected. To create this button follow the following steps: 

Step 1: Write the code. Using vbAbortRetryIgnore creates three-button Abort, Retry and Ignore. vbDefaultButton2 is the code that helps us select the second default button i.e. Retry. 

Step 2: The MsgBox appears and the default is Retry. 

Different Icons in Excel VBA

While working with basic excel. You have come across different warnings and information. These all are represented with symbols. For example, a yellow exclamation for warning, a blue color question marks for information, etc. Here, we have discussed some of the icons that can appear in a message box. 

1. vbCritical: This icon is a red cross. This red cross is used to display a critical message. To create this icon follow the following steps: 

Step 1: Write the code. vbCritical creates a red cross. 

Creating-vbCritical

Step 2: The message box with a critical icon appears.

vbCritical-is-created

2. vbQuestion: This icon is a blue question mark. This blue question mark is used to display when you forgot something to choose in a procedure. To create this icon follow the following steps: 

Step 1: Write the code. vbQuestion creates a blue Question Mark.

Creating-vbQuestion-icon

Step 2: The message box with a Question icon appears.

vbQuestion-icon-is-created

3. vbExclamation: This icon is a yellow exclamation mark. It is used to represent an error when the entered data doesn't match with the already present data. To create this icon follow the following steps: 

Step 1: Write the code. vbExclamation creates a yellow exclamation mark. 

Create-vbExclamation-icon

Step 2: The message box with a ! (exclamation) icon appears. 

vbExclamation-is-created

4. vbInformation: This icon is a blue I mark that represents information. To create this icon follow the following steps: 

Step 1: Write the code. vbInformation creates a blue I information mark.

vbInformation-is-creating

Step 2: The message box with an i(information) icon appears. 

vbInformation-is-created

Values returned by Buttons

Till now, you have studied how to create a message box and the different buttons associated with it. Now, we will learn how you can create nested message boxes and also with some conditions that are more practical in the real designing of a dialogue box. A message box returns a code or a constant according to the buttons applied. Some of the most commonly used constants and their return value are given in the table below. 

ConstantValue
vbOKOnly0
vbOKCancel1
vbAbortRetryIgnore2
vbYesNoCancel3
vbYesNo4
vbRetryCancel5

Note: You can use values as well as constant interchangeably while comparing the boolean expressions.

For example, given Yes, No, and Cancel buttons. Display a message box accordingly. So to do this task follow the following steps:

Step 1: Write the code. The initial MsgBox will return a value and it will get stored in a variable name ans. If you click yes, then it will go inside the if condition else it will output the other one. Click on the Run button. 

Write-the-code

Step 2: A dialogue box appears. Click on Yes.

Click-on-yes-button

Step 3: A dialogue box appears. Click Ok. 

Click-on-Ok-button

Editing Title in a Msg Box

You can edit the title in a message box. By default, it shows Microsoft Excel as the title. In the MsgBox, function the third argument is for writing your custom title. It is a temporary argument. Follow are the following steps: 

Step 1: Write the code. The third argument is your custom title. 

Editing-the-title

Step 2: A dialogue box with the title "Be a geek" appears. 

Title-of-the-message-box-is-changed


Next Article
MessageBox in Excel VBA

G

gautamgoel962
Improve
Article Tags :
  • Excel
  • Geeks-Premier-League-2022
  • Excel-VBA
  • ExcelGuide

Similar Reads

    VBA Strings in Excel
    In Excel's Visual Basic for Applications(VBA), strings are pivotal in handling and manipulating text-based data. Strings serve as a fundamental data type used to store a sequence of characters, enabling the representation of textual information, numbers, symbols, and more. Understanding how VBA hand
    8 min read
    Sub Procedure in Excel VBA
    A Sub Procedure is an action that is written in VBA code. It starts with a Sub statement and ends with an End Sub statement. It is used to manipulate the data in an Excel sheet which includes a table present in an Excel sheet or a pivot table present in the Excel sheet. The name of the procedure sho
    3 min read
    VBA Print Statement in Excel
    When there are many columns and rows in excel but we only want to save or print a few of them because that few rows and columns are important to us, at that case we will print them in Excel VBA so that we can fetch those details later and also to get the print out of that table because daily life ha
    2 min read
    Excel VBA | sum() functions
    Visual Basic for Applications (VBA) is the programming language of Excel and other offices. It is an event-driven programming language from Microsoft. With Excel VBA one can automate many tasks in excel and all other office software. It helps in generating reports, preparing various charts, graphs a
    2 min read
    InputBox Function in Excel VBA
    Input Box is one of the most frequently used functions in VBA Macro. The dialogue box that appears asks for input from the user and returns a constant/code/number/text. For example, if you want to create a log-in form in VBA, you will require an input box function. Let's learn how to create an input
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences