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.
- 00:05 Over the next few modules, we are gonna look at how to collect feedback
- 00:08 from users and how to actually pop up messages to display what's going on.
- 00:13 And the primary character that we use for doing that is the VBA MsgBox.
- 00:18 To create a message box is actually relatively straight forward.
- 00:20 Every message box we're going to use takes about four parts altogether.
- 00:24 The first one is the actual call for the msgbox and you'll hit a space and
- 00:29 put in a quote because we're working on the prompt.
- 00:33 So the first part we get is a message here, so
- 00:35 I'm gonna type in something that says I am a Message box close my quotes.
- 00:40 When I hit comma, it gives me the options for the message box style
- 00:45 we're gonna use and I'm going to work with one called VB okay only and
- 00:49 we'll look at a lot of these in the next couple of minutes here.
- 00:53 I'm going to then hit comma and it asks me to provide a custom title for
- 00:58 the message box.
- 00:59 So if I leave this blank it will just come up with a title of Microsoft Excel, but
- 01:02 I'd rather have something of my own.
- 01:04 Something that says maybe hello.
- 01:07 And at this point when I arrow down you'll notice that re-indents the line,
- 01:10 everything looks good, not syntax errors or complaints.
- 01:13 So at this point, I'm going to see how I run this.
- 01:15 At this point, I'm going to click going to click in the middle here.
- 01:18 And I'm just gonna click run arrow and
- 01:20 you'll notice that it pops up a message box that says, Hello I am a message box.
- 01:24 There we go. So
- 01:25 that easy to create your very first basic message box.
- 01:29 One big key the reason why have Excel doc on the left hand side here because
- 01:33 the message box actually doesn't show in the VBA window,
- 01:36 it shows in the Excel window.
- 01:37 So if I actually have I have my VBA window maximized at this point in time,
- 01:41 it would have actually kicked me back over to Excel because I have Excel
- 01:45 showing a little bit on screen here it will actually pop it up in front.
- 01:48 So, that works out nicely for me.
- 01:51 Now, what if we want to actually have a line break and
- 01:54 the message starts to get really long.
- 01:56 Well, we actually have four different character sets that we can use in order to
- 02:00 create a line break.
- 02:02 And you'll notice in this case here that I have my message box set up it says that I
- 02:05 want this or want this message, and
- 02:07 then we've got this closed quotes and we've got this and vb new line and.
- 02:12 And then we open the quotes again, say, show on two lines.
- 02:15 And if you look down each line, we've got vb new line.
- 02:18 We've got another consonant here called vbcr,
- 02:20 which is a visual basic carriage return.
- 02:22 A vblf, which is the visual basic line feed or the vb.
- 02:26 CRLF which is the Visual Basic character term NYE characters.
- 02:31 So if I go and
- 02:32 run this right now, you'll see what these look like is breaks this out.
- 02:35 And every one of them is completely identical.
- 02:37 Also, notice the okay only style I did not declare the okay only here.
- 02:42 That's the default, so it shows up here.
- 02:44 So we say,
- 02:45 OK and you'll notice that every single one of these looks absolutely identical.
- 02:50 So, why do we have four different characters?
- 02:52 Well, the answer is that if you're working with Windows, most of the time you're
- 02:55 gonna use vbNewLine or vbCrLf although the others work.
- 02:59 If you're trying to port code to run on a Mac,
- 03:01 you may need to actually run with vbCr or vbLf.
- 03:04 You have to test it to see.
- 03:07 Now, let's just watch the buttons.
- 03:09 You'll notice here that we've got something set up that's giving me
- 03:13 the message I'm using, vbOKOnly, and this is the constant in the middle, vbOKOnly.
- 03:16 And I've got a whole bunch of them OKCancel, RetryCancel.
- 03:19 So, let's take a look at what all these look like.
- 03:22 This is vbOKOnly.
- 03:24 It's got an OK button and that's it.
- 03:26 This is OK, Cancel.
- 03:29 And it actually doesn't matter which button I click at this point in time.
- 03:31 It's just gonna take me to the next screen,
- 03:33 because I'm not trying to capture any input from them.
- 03:35 So I could hit OK or Cancel, it's gonna move forward.
- 03:37 But that's the vbOKCancel style.
- 03:40 This is the vbRetryCancel style, so we've got a Retry and a Cancel button.
- 03:44 We've got the vbYesNo.
- 03:46 VbYesNoCancel.
- 03:51 AbortRetryIgnore and those are the styles you generally gonna end up using to
- 03:56 control which buttons show up on your form.
- 04:01 Now, the next one we have is we also have some options here.
- 04:05 So this one, if we actually take a look at it, instead of using the OKonly,
- 04:09 Cancel and Retry stuff like that.
- 04:11 I've got four different styles that we can actually uses well to
- 04:14 show different icons.
- 04:15 So this is a pb question.
- 04:17 We've got a question mark here.
- 04:19 We've got information which shows little i in the circle, we've got the VB
- 04:23 exclamation which gives us the exclamation part and a point in the yellow triangle
- 04:28 and VB critical, and this is generally the one that we use for stop again notice I've
- 04:32 declared any title so we've got Microsoft Excel as the default here.
- 04:37 Now, this is cool, but
- 04:38 wouldn't it be nice if we could add the icon with the different buttons?
- 04:42 And we can absolutely do that as well.
- 04:44 The way that we do that is that actually we go through and
- 04:47 we add the two constants together.
- 04:49 So, we've got vbCritical + vbOKOnly.
- 04:53 The other thing I should call out here to is that we actually have a secondary
- 04:56 character here that's breaking down this command under multiple lines.
- 05:00 So, the space underscore is a line break in your code.
- 05:03 So, that you can actually line things up.
- 05:05 This is exactly the equivalent of doing this.
- 05:08 So, if I've got space underscore Enter and then I can tab it over.
- 05:13 This'll all be treated as one line when I run it.
- 05:16 So let's go take a look at what this looks like now.
- 05:20 This is the vbCritical in vbOkOnly.
- 05:22 So we've got the Ok button only and we've got the critical Icon.
- 05:28 This one here is slightly different we got the Yes No and we got the question icon.
- 05:32 So we have a little bit control of how we actually design our message boxes actually
- 05:37 look so that's the fundamental layer of how message box is can be configure to
- 05:41 look, and then our next module will go around and
- 05:43 figure out how we can actually read the input back.
Lesson notes are only available for subscribers.