Excel VBA MsgBox basics.

June 2017


MsgBox Function

Displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked.

Syntax

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

contextDescription
prompta (Must) string, The maximum length of prompt is approximately 1024 characters, If prompt consists of more than one line, you can separate the lines using a carriage return character (Chr(13)
buttonsOptional, the sum of values specifying the number and type of buttons to display, the default value for buttons is 0.
titleOptional. String expression displayed in the title bar of the dialog box.
helpfileOptional. String expression that identifies the Help file to use to provide context-sensitive Help for the dialog box.
contextOptional. Numeric expression that is the Help context number assigned to the appropriate Help topic by the Help author.


Examples
Dim m
m = MsgBox("Simple MsgBox")


Dim m
m = MsgBox("Simple MsgBox with a custom title", 0, "This is a test Title")


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 1)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 2)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 3)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 4))


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 5)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 16)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 32)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 48)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 64)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 256)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 512)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 768)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 4096)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 16384)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 65536)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 524288)


Dim m
m = MsgBox("Line-1" & Chr(13) & "Line-2", 1048576)

Return Values

ConstantValueDescription
vbOK1OK
vbCancel2Cancel
vbAbort3Abort
vbRetry4Retry
vbIgnore5Ignore
vbYes6Yes
vbNo7No


Example
Sub test()
    Dim m
    m = MsgBox("Do you really want do delete current active cell's value?", 4, "Delete confirmation")
    If m = 6 Then
        ActiveCell.Value = ""
    End If
End Sub

in this example if you choose "Yes" then the active cell will be erased.

Related


Published by MaxStart. Latest update on April 7, 2015 at 10:22 PM by MaxStart.
This document, titled "Excel VBA MsgBox basics.," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).