Talking to the User: Message Box and Input Box

1.        MsgBox

MsgBox (Message Box) brings up a dialog box with a title bar, a message of our choice, and a choice of icons and buttons.  The function can return a value that tells us which button the user clicked.

We have seen MsgBox in passing in earlier posts.  Here is the syntax of the MsgBox statement:
MsgBox prompt [, buttons] [, title]

and, of the MsgBox Function:
ReturnValue = MsgBox( prompt [, buttons] [, title] )

The buttons argument settings are:

ConstantValueDescription
vbOKOnly0Display OK button only (default).
vbOKCancel1Display OK and Cancel buttons.
vbAbortRetryIgnore2Display Abort, Retry, and Ignore buttons.
vbYesNoCancel3Display Yes, No, and Cancel buttons.
vbYesNo4Display Yes and No buttons.
vbRetryCancel5Display Retry and Cancel buttons.
vbCritical16Display Critical Message icon.
vbQuestion32Display Question icon.
vbExclamation48Display Warning Message icon.
vbInformation64Display Information Message icon.
vbDefaultButton10Make First button the default (it already is)
vbDefaultButton2256Make Second button the default.
vbDefaultButton3512Make Third button the default.
vbApplicationModal0Application modal; the user must respond to the message box before continuing work in the current application (default).
vbSystemModal4096System modal; all applications are suspended until the user responds to the message box.
vbMsgBoxHelpButton16384Adds Help button to the message box
VbMsgBoxSetForeground65536Specifies the message box window as the foreground window
vbMsgBoxRight524288Text is right-aligned

You can add one constant from each group to form the buttons parameter.  E.g. vbYesNoCancel + vbQuestion + vbDefaultButton3

For the MsgBox Function, you can tell which button the user pressed.  Compare the ReturnValue against these built-in constants:

ConstantValueButton chosen
vbOK1OK
vbCancel2Cancel
vbAbort3Abort
vbRetry4Retry
vbIgnore5Ignore
vbYes6Yes
vbNo7No

For example, using vbNo:
If MsgBox("Do you want to proceed?", vbQuestion + vbYesNo) = vbNo Then Exit Sub

Here, the MsgBox displays the Question icon (vbQuestion) and the Yes and No buttons (vbYesNo).  If the user clicks No (ReturnValue equals vbNo), the Sub ends.

2.        InputBox$

This is a bit different to MsgBox in that it allows you to ask the user to input a string.  There are always OK and Cancel buttons.  Pressing Cancel returns a blank string (“”).  So does clicking OK without typing anything.  It is advisable to check for this before further processing. 

The syntax is:
ReturnSt$ = InputBox$(prompt[, title][, default][, xpos][, ypos]])

You can omit any argument except the prompt.  But if you do use one, you must use all the preceding commas too.  It is particularly useful to provide a default value.

In both MsgBox and InputBox, you can force new lines in your message by adding in the constant vbLf, for example:
Nam$ = InputBox(“What is your name?” & vbLf & vbLf & _
“(Click Cancel if you don't know)”, “Name Entry Box”, PrevNam$)

3.        Example

You can get the file Macros.xlsm free of charge from us.  If you have, Click the button Message Demonstration on Sheet1.  Repeat it a few times, taking different choices.  Do not forget to see how it behaves if you enter something that is not a number.

Then press Alt+F11 for the VB Editor and look in the Module Code_Examples for the Sub Messages.

Otherwise, copy and paste the code below into a blank worksheet and then run it.

Sub Messages()    ' Message Demonstration:
  Dim St$
  If vbNo = MsgBox("Would you like to proceed?", _
    vbQuestion + vbYesNo, "The Inquisition!") Then Exit Sub
  St$ = InputBox("Gimmie a Number, kid!", "Any number will do!")
  If St$ = "" Then
    MsgBox "Hey!  You hit cancel!", vbExclamation, "Coward!"
  ElseIf IsNumeric(St$) Then
    MsgBox "Thank you for: " & St$, , "Number OK"
  Else
    MsgBox "Since when is '" & St$ & "' a Number?", vbCritical
  End If
End Sub

This procedure first uses a MsgBox function to return a vbYes or vbNo value (set using vbYesNo).  If it is a vbNo, the program ends with an Exit Sub.  Otherwise, an InputBox asks for a number, which goes into the string St$.  That string is checked against blank (“”).   If it is blank, the program displays a MsgBox with the vbExclamation icon, and drops through to End Sub.

Otherwise, the program uses the IsNumeric() function to check if the string is numeric.  In either case, it then responds with an appropriate message.

Similar Posts