VBA HOME PAGE

Menu

Recording macros

Looking at the code

Ways of running macros

Where macros are stored

Reasons to write macros

Writing macros

Procedure types

Visual Basic editor (VBE)

Rules & conventions

Excel objects

Range/Selection objects

Object hierarchy

Object browser

Chart objects

Pivot Table objects

Formulas

Visual Basic Functions

Creating Add-Ins

Variables & constants

Object variables

Arrays

Collections

Message Box

VBA Input Box

Excel Input Box

Making decisions (If)

Making decisions (Case)

Looping (Do...Loop)

Looping (For...Loop)

With...End With blocks

User defined functions

Event handling

Error handling

Debugging

Creating User Forms

DAO/ADO Objects

Input/Output Files

 

Other links

Example code snippets

Userform input example

 

 

 

 

 


Message Box (MsgBox)

A Message Box (MsgBox) displays a message alert box with optional set of buttons, icon and other arguments settings

.

There are two types of message boxes:

1. MsgBox Statement - One way communication to the user.

 

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

 

2. MsgBox Function - Two way communication which the system returns a value.

 

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

 

Example1 - MsgBox Statement:

Sub MessageBoxTest()
   
'brackets are not required for a statement
   
MsgBox "The task is now complete"
End Sub

Example2 - MsgBox Function:

Sub MessageBoxTest ()
    Dim answer
    answer =
MsgBox("More Input", vbYesNo + vbQuestion, "Data Entry")
End Sub

In the above example, the vbYesNo is the command used to create the Yes and No buttons and the + vbQuestion is the command to create the Question Mark image.

 

§   If the user clicks on Yes, the message box will return the constant vbYes.

§   If the user clicks on No, the message box will returns the constant vbNo.

 

Example2 - MsgBox Function (Multiple Lines):

Sub MultiLineMessageBox()
    Dim intResponse As Integer
    intResponse = MsgBox("You have chosen to save this file." _
        &
vbNewLine & "Do you wish to proceed?" & vbNewLine & _
        "Click 'Yes' to save or 'No' to close and not save.", _
        vbQuestion + vbYesNoCancel, "Save File")
    ....................................
End Sub

The above example will display multiple lines in the message box using the constant vbNewLine.

 Constants for MsgBox

Buttons and icons are combined for the Buttons argument which have a unique value that drives the output of how users use and see button combinations:

Constant

Value

Description

vbOKOnly 0 Display OK button only.
vbOKCancel 1 Display OK and Cancel buttons.
vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.
vbYesNoCancel 3 Display Yes, No, and Cancel buttons.
vbYesNo 4 Display Yes and No buttons.
vbRetryCancel 5 Display Retry and Cancel buttons.
vbCritical 16 Display Critical Message icon.
vbQuestion 32 Display Warning Query icon.
vbExclamation 48 Display Warning Message icon.
vbInformation 64 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 Application modal; the user must respond to the message box before continuing work in the current application.
vbSystemModal 4096 System modal; all applications are suspended until the user responds to the message box.
vbMsgBoxHelpButton 16384 Adds Help button to the message box.
VbMsgBoxSetForeground 65536 Specifies the message box window as the foreground window.
vbMsgBoxRight 524288 Text is right aligned.
vbMsgBoxRtlReading 1048576 Specifies text should appear as right-to-left reading on Hebrew and Arabic systems.

 

The following applies to the 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

 

While a the MsgBox is being displayed, the macro procedure is paused waiting for the user to click a button whether it is a statement or a function.

 

Note the difference between the two types regarding when parenthesis are used and can be ignored. Also, be aware any function must be placed to the right side of an = (equal sign) because it returns an answer.

 

 

 


Next Topic: VBA Input Box

 

Want to teach yourself Access? Free online guide at About Access Databases

VBA Keyword: MsgBox
Home | Terms of Use | Privacy Policy | Contact

© copyright 2010 TP Development & Consultancy Ltd, All Rights Reserved.

All trademarks are copyrighted by their respective owners. Please read our terms of use and privacy policy.