MsgBox and InputBox: messages and user input

MsgBox lets your macro show a message, such as "Done" or a question with Yes and No. InputBox asks for a value that the user types in. Here you learn both, how to use the answer and what the parentheses are about.

In short: MsgBox "Done." shows a message. answer = MsgBox("Continue?", vbYesNo) returns 6 for Yes and 7 for No. entry = InputBox("How many?") returns the text that was typed.

What you need to know: Variables, because you store answers and input in variables.

Try it right now

The macro should show the contact from B2 in a message. You join the fixed text in front with &, the operator that sticks pieces of text together. & gets a proper look in Calculations and text.

Task Show the contact from B2 in a message, with this text in front: "Contact: Jana Wolf" (exactly like that, with a colon and one space). The macro reads the name from B2.

ABCD
1CustomerPeters Motors
2ContactJana Wolf
3Phone0161 496 0732
4
5

How MsgBox is built

MsgBox "Contact: " & Range("B2").Value
MsgBox "Report created.", vbInformation, "Monthly report"
ArgumentMeaningIn the example
PromptThe text of the message. Required."Report created."
ButtonsWhich buttons and which icon. Without it, just OK.vbInformation
TitleText in the title bar. Without it, "Microsoft Excel"."Monthly report"

The message pauses the macro until someone clicks. For a line break inside the message, use vbNewLine: MsgBox "Line 1" & vbNewLine & "Line 2".

ConstantEffect
vbOKOnlyOK only (default)
vbYesNoYes and No
vbYesNoCancelYes, No and Cancel
vbOKCancelOK and Cancel
vbQuestion, vbInformation, vbExclamation, vbCriticalIcon: question mark, info, warning, stop

Combine buttons and icon with a plus: vbYesNo + vbQuestion.

InputBox: ask for a value

Dim boxes As String
boxes = InputBox("How many boxes arrived?")
Range("B3").Value = boxes
MsgBox boxes & " boxes recorded."

InputBox shows a window with your prompt, a text box, OK and Cancel. Whatever the user types comes back as text. If you write that text to a cell and it looks like a number, Excel stores a number, just as if it had been typed. If the user clicks Cancel, InputBox returns empty text, "". The second argument is a title, the third a default that already sits in the box: InputBox("Month?", "Report", "March").

Task Use InputBox to ask "How many boxes arrived?", write the answer to B3 and then confirm with the message "24 boxes recorded." (with the number that was entered instead of 24). When you run it, the exercise types 24; Check uses a different number.

ABCD
1Goods in
2SupplierNorthside Paper Co.
3Boxes
4
5

Yes or No: the return value

MsgBox can ask a question, and then you want to know what was clicked. For that, the arguments go in parentheses and a variable goes on the left:

Dim answer As Long
answer = MsgBox("Is the delivery complete?", vbYesNo)
ClickReturnsConstant
OK1vbOK
Cancel2vbCancel
Yes6vbYes
No7vbNo

Compare with the constants rather than the numbers: answer = vbYes reads better than answer = 6. Turning the answer into a decision is what If Then Else is about. In the exercise you first write the answer to a cell, so you can see what comes back.

Task Ask with a Yes/No message: "Is the delivery complete?" Write the answer, the return value of MsgBox, to B2. When you run it, the exercise clicks Yes; Check clicks No.

ABCD
1Delivery noteDN-204316 = Yes, 7 = No
2Complete
3
4
5

Parentheses: with or without?

The rule: if you need the result, put the arguments in parentheses (answer = MsgBox("Continue?", vbYesNo)). If you do not, leave them out (MsgBox "Done.", vbInformation). With a single argument, MsgBox("Done.") also works, but two arguments in parentheses without an assignment give a compile error in the real VBA editor.

Common mistakes

Return value without parentheses

answer = MsgBox "Continue?", vbYesNo causes "Compile error: Expected: end of statement". When a variable is on the left, the arguments belong in parentheses.

Missing space in the fixed text

"Contact:" & Range("B2").Value gives "Contact:Jana Wolf", and Check tells you the message differs from the expected "Contact: Jana Wolf". The space goes before the closing quote.

Typing the text in

MsgBox "Contact: Jana Wolf" is only right for the data you can see. Check puts a different name in B2, and the message no longer fits.

Forgetting about Cancel

If someone clicks Cancel in the InputBox, empty text comes back and the macro writes an empty cell. If can catch that: If boxes = "" Then Exit Sub. That is coming up next.

In real Excel

Messages are useful for telling users what happened, but they stop the macro. A macro that works through a hundred rows should not show a MsgBox for each one. While developing, Debug.Print is the better way to look at values: it writes to the Immediate window (Ctrl+G) without stopping. For number input there is also Application.InputBox with Type:=1, which only accepts numbers.

FAQ

Can I change the button labels?

No. MsgBox only offers the fixed combinations such as Yes/No or OK/Cancel, in the language of Office. Custom buttons need a UserForm, a window of your own, which this course does not cover.

Why does InputBox return text when I type a number?

InputBox does not know what you expect and always returns text. To calculate with it, convert it, for example with CLng(boxes), or use Application.InputBox with Type:=1.

How long can a MsgBox text be?

According to Microsoft, about 1,024 characters, depending on how wide the characters are. For longer texts a message box is the wrong tool anyway; write them to a sheet.

Learn this next: calculations and joining text

Arithmetic, integer division, remainders with Mod and joining text with &, plus what happens to decimals when numbers become text.

Practice calculations in VBA

Write to us

Choose language

Select your preferred language