Locked lesson.
About this lesson
How to provide feedback to your user via the VBA MsgBox object.
Quick reference
Creating VBA Message Boxes
Displaying messages and collecting user feedback
When to use
To display messages to users
Instructions
MsgBox Syntax
MsgBox "Message Text", <msgBoxStyle> + <msgBoxButtons>, "Message Box Title"
MsgBox Style Constants
vbQuestion | displays a question mark in a blue circle |
vbInformation | displays an “i” in a blue circle |
vbExclamation | displays an exclamation point in a yellow triangle |
vbCritical | displays an “x” in a red circle |
MsgBox Button Constants
- Combine MsgBox styles and buttons by using the plus sign (+)
vbOKOnly | displays button with OK |
vbOKCancel | displays buttons with OK and Cancel |
vbRetryCancel | displays buttons with Retry and Cancel |
vbYesNo | displays buttons with Yes and No |
vbYesNoCancel | displays buttons with Yes, No, and Cancel |
vbAbortRetryIgnore | displays buttons with Abort, Retry, and Ignore |
Adding line breaks to MsgBox text
Syntax
MsgBox “Message Text” & <LineBreakConstant> & “Remaining Message”
Line Break Constants
vbNewLine |
Visual Basic New Line |
vbCr |
Visual Basic Carriage Return |
vbLF |
Visual Basic Line Feed |
vbCrLf |
Visual Basic Carriage Return & Line Feed |
Hints & tips
- If a title is not set, the default title at the top of the box will be "Microsoft Excel".
- The default button style for a message box vbOKOnly, displaying an OK button.
- The message box appears in the Excel window, not the VBA window.
- Line code up more cleanly by using the line continuation character (space + underscore) and indenting.
Lesson notes are only available for subscribers.