Vous êtes sur la page 1sur 13

Message Box Function in VBA

If we enter a wrong password in facebook:


We get a message The password youve entered is incorrect?

And then we just re-type the password. Easy!

http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

In fact, its so easy that we dont even need that message.We just intuitivelyunderstand that
we might have entered a wrong password, and weve to re-enter it.
But if someone is new to Facebook, he actually needs that message. Otherwise, how hell be able
to know what went wrong? And what he is supposed to do now?
Bottom line : Ifthere was no such message, it might have been a little difficult to understand what
were supposed to do now.
With me?
Whats true for Facebook is also true for Macros.
Fortunately, to inform the user what is going onwe have a similar function in VBA that informs
the user by displaying a message box. This function is known as MsgBox.
To understand how MsgBox function can be usefullets take Johns example.
John runs a small business and maintains his clients data in an excel workbook.But sometimes,
he mistakenly writes only 9 digits(instead of 10) while saving his clients mobile number.
And unfortunately, he only becomes aware of it, when its too late.
Wouldnt it be nice, if John could create a macro that instantly displays a message as shown
below?

Well it is!
http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

So, in this chapter, well help John by making that macro for him.
But before that, lets quickly learn the basics of Message box function, its anatomy and syntax
and how to use it in VBA.

MsgBox FunctionsSyntax And Anatomy


Message box function has the following syntax :MsgBox (Text , ButtonTypes+IconStyle, Title, Helpfile, Context_Id)

PARAMETERS WHAT THEY ARE?


The text inside the red rectangle(Welcome) is the Text parameter of MsgBox
Function.

Text(Prompt)

This defineswhat type of buttons you want to use in your message box. If it

ButtonTypes doesnt make any sense to you right now, dont worry. It will be discussed in
detail shortly.

IconStyle

This defines which icon you want to show in your message box. And this too
will be discussed shortly.
The text you seeing inside the red rectangle(Microsoft Excel) is the Title of

http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

message box.

Title

If you create a simple macro, you wont need this parameter. But if you create
an application for others in VBA, you might want to include a text file for
helping users.
Anyways, in this parameter all you need to do is just provide the path of that

HelpFile

help file. But that file must be in .chm (Compiled html) format otherwise it
wont work. You can easily learn to create a .chm file, here well only
Creating a .chm file is off topic, so we wont discuss it here, You can easily
learn to create a .chm file(just google it) Here well only discuss about using
that file in your code.

When youll learn about making .chm help files, youll also learn about
context id. Basically a context id is just a number that you assign to every
topic while making a .chm help file. For example, lets say you create a help
file about using Facebook, and it has five (Topics or Pages) like (1) How to
sign up. (2) How tolog in. (3) What to do if you forget your password. etc. So,
you might want to show only relevant information according to the context a

Context_Id

user is in. If hes just registering as a new user, you want to show him page 1
when he click on help, and if he has entered a wrong password you might want

http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

to show him page 3 if he click on help. So all you need to do is give ever topic
a number it may be 1, 2, 3 or it may be 101, 105 whatever you like. (Youll
learn about it, when youll learn your own .chm file. So Ill limit myself here.
In short, context_id is just the number you assigned to a topic while creating a
.chm file, and you use that number here.

It might make no sense to you at this point, so lets create a simple macro to make things clear :
1. Press Alt+F11 to open VBE (Visual Basic Editor)
2. Right click anywhere on Project Explorer window.

http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

3. Then go to Insert, and from drop down menu select ->Insert Module.
4. Finally type the following code in

Sub MessageBox_Example()
MsgBox "excelmacrotutorial.com is Awsome!"
End Sub

Then press F5 key to run this macro, and youll get :

Congratulations! Youve successfully used MsgBox in your macro. If youve noticed, we only
used first parameter(Text) while writing code for MsgBox function, we ignored the other
parameters (i.eButtonTypes+IconStyle, Title, Helpfile, Context_Id) because only the first
parameter is necessary. All other are just optional, you might use them if you want.
Now, I want to draw your attention to the second parameter i.e ButtonType. In VBA, weve total
six types of Buttons to chose from :BUTTON NAME

HOW IT LOOKS

vbOKOnly

Display OK button only.

vbOKCancel

Display OK and Cancel buttons.

vbAbortRetryIgnore

Display Abort, Retry, and Ignore buttons.

http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

vbYesNoCancel

Display Yes, No, and Cancel buttons.

vbYesNo

Display Yes and No buttons.

vbRetryCancel

Display Retry and Cancel buttons.

In addition to above six Button types, weve the following Icon types as well, you may chose any
of your liking :

ICONSTYLE

HOW IT LOOKS

vbCritical

Display Critical Message icon.

vbQuestion

Display Warning Query (question mark) icon.

vbExclamation

Display Warning Message icon.

vbInformation

Display Information Message icon.

Great!
Now youve learned about Second parameter and third parameter as well(Button Type and Icon
Style). Lets use them in a code.
Again Go through the following steps :
1. Press Alt+F11 to open VBE (Visual Basic Editor)
2. Right click anywhere on Project Explorer window.

http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

3. Then go to Insert, and from drop down menu select ->Insert Module.
4. Finally type the following code in

Sub MessageBox_Example()
MsgBox "excelmacrotutorial.com is Awsome!", vbAbortRetryIgnore
End Sub

Then press F5 key to run this macro, and youll get :

http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

Well Done! You just learnt using button in your code.


Did you noticed that in our first example we didnt defined the button type still, excel displayed a
OkOnly button? Thats because its a default button type(means even if you dont mention it,
excel will show it)
Now go ahead and create another macro, this time use vbYesNo button type. Ill wait.
Done? When you run that macro youll get a message box like this :

Similarly you can chose any of the button type as desired among the six button types already
mentioned in this chapter.
By now it will be making more sense to you, isnt it?
Now I want you to use third parameter in your code i.e Icon Style.

http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

Again Go through the following steps :


1. Press Alt+F11 to open VBE (Visual Basic Editor)
2. Right click anywhere on Project Explorer window.

3. Then go to Insert, and from drop down menu select ->Insert Module.
4. Finally type the following code in

Sub MessageBox_Example()
MsgBox "excelmacrotutorial.com is Awsome!", vbYesNo +
vbCritical

http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

End Sub
Then press F5 key to run this macro, and youll get :

So, what we did here? We just use a + sign and wrote our icon style button from the list in front
of it, and boom. Our message box got a critical icon(The cross icon inside the red rectangle)
Now weve learned how to use the first three parameters. Youre ready to move to the forth
parameter i.e Title
Again Go through the following steps :
1. Press Alt+F11 to open VBE (Visual Basic Editor)
2. Right click anywhere on Project Explorer window.

http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

3. Then go to Insert, and from drop down menu select ->Insert Module.
4. Finally type the following code in

Sub MessageBox_Example()
MsgBox "excelmacrotutorial.com is Awsome!", vbYesNo +
vbCritical, "Excel Macro Tutorial"
End Sub

http://excelmacrotutorial.com/lesson-no-6-message-box/[9/30/2016 8:12:48 PM]

Lesson No. 6 : Message Box

Then press F5 key to run this macro, and youll get :

Notice the text inside the red rectangle?That is the title parameter showing up thereI hope by
now it will clear to you how to use the above those parameters.

Vous aimerez peut-être aussi