Chapter 5 – MsgBox and InputBox

Chapter 5 – MsgBox and InputBox

In this chapter, we will learn how to use common Input and Output methods in VBA. We will also see practical examples of each method.

MsgBox

Msgbox function is used to show an alert or message to user in form of dialog box.  This function is normally used to show confirmation of the action taken or take inputs from user in form of yes or no.

VBA for Beginners

Commonly used Buttons in MsgBox:

Button Type Value Description
vbOKOnly 0 Displays OK button
vbOKCancel 1 Displays OK and Cancel buttons
vbAbortRetryIgnore 2 Displays Abort, Retry and Ignore buttons
vbYesNoCancel 3 Displays Yes, No and Cancel buttons
vbYesNo 4 Displays Yes and No buttons
vbRetryCancel 5 Displays Retry and Cancel buttons
vbCritical 16 Displays Critical Message icon
vbExclamation 48 Displays Warning Message icon
vbInformation 64 Displays Information Message icon

 

Example 1: A normal message box

Code:

MsgBox ("World is beautiful")

Result:
VBA for Beginners

Example 2: Message box with Yes and No buttons

Code:

MsgBox "Are you sure?", vbYesNo

Result:
VBA for Beginners

Example 3: Message box with Title

Code:

MsgBox "Are you sure?", vbYesNo, "Confirmation"

Result:
VBA for Beginners

Example 4: Message box with Information Message Icon

Code:

MsgBox "Task is completed", vbInformation, "Confirmation"

Result:
VBA for Beginners

Example 5: Message box with Critical Message Icon

Code:

MsgBox "Task could not be completed", vbCritical, "Error"

Result:
VBA for Beginners

Example 6: Message box with Abort, Retry and Ignore buttons

Code:

MsgBox "Task could not be completed" & vbNewLine & "Want to retry?", vbAbortRetryIgnore, "Error"

Result:
VBA for Beginners

Displays a dialog box to accept input from user. This function is normally used to take simple input from user in form of string.

InputBox

VBA for Beginners

Example 1: Simple Input dialog box

Code:

InputBox ("What is your name?")

Result:
VBA for Beginners

Example 2: Input box with title and default value

Code:

InputBox "How many sheets to be added?", "Sheets", 3

Result:
VBA for Beginners

Author:
Excelsirji.com was started with the thought of making people experience easy while working with Excel, data analytics, vba, automations etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a Data Enthusiast?

Join us for a ride on your data and automate your stuffs

Business Analytics
Data Analyst
Programming for Data Science
Predictive Analytics for Business