Anda di halaman 1dari 6

Chapter 4 - Talking to the User

CHAPTER 4 - TALKING TO THE USER


4.1

Displaying Messages in the Status Bar

You can use the status bar to keep your user informed of the progress of a macro:
The great advantage of status bar messages is that
they do not interrupt your macro the great
disadvantage is that no one tends to notice them!

Displaying the Status Bar


To display a message in the status bar, set the StatusBar property of the Excel Application:

The first time round the loop Excel will


set the status bar to read:
On number 1

Resetting the Status Bar to Blank


To unset the status bar, just set it to False!

Application.StatusBar = False

When you turn the application status


bar off, you see the standard Ready
message again.

Copyright 2016

Page 25

Chapter 4 - Talking to the User

4.2

Displaying Messages on Screen

Syntax of the MsgBox Command


You can use the MsgBox command to display a message on screen. The arguments are:
Named Arguments

Details

Prompt

The prompt which appears in the message box

Buttons

What command buttons appear

Title

The title of the message box

Here is an example of the use of MsgBox to display a


message to your user:

With a little more effort, you can customise the title of


the dialog box:

This simple subroutine displays


an equally simple message:

To specify the Title argument


we put an extra comma for the
missing Buttons argument:

Building Up Messages
You can use the & symbol to concatenate (or join
together) the different parts of a string:

Wise
Wise Owls
Owls
Hint
Hint
Copyright 2016

This message will display the


current year whenever it is run.:

This is one of the few occasions in VBA when blank spaces matter you
must put a space before and after each & symbol.

Page 26

Chapter 4 - Talking to the User

Multiple Line Messages


Use the VbCrLf constant to specify a carriage return in a message:

To get blank lines to appear, specify them as part of the string to be displayed. VbCrLf
stands for Visual Basic Carriage Return Line Feed, if that helps you remember!

Customising your Message Box


The diagram below shows the main ways in which you can customise a message box:
The Title is different (weve already seen how to do this).

This exclamation mark is not the default symbol shown.


These arent the default set of buttons, and the button
should be the one selected by default.

The macro to display the dialog


box shown. Notice that weirdly
- you add together the button
choices.

Copyright 2016

Page 27

Chapter 4 - Talking to the User

Button Combinations Available


The combinations of buttons
available are shown here:

Constant to use

What buttons you get

VbOKOnly

Just the

button

OK

VbOKCancel

OK

and

VbAbortRetryIgnore

Abort

VbYesNoCancel

Yes

, No and

VbYesNo

Yes

and

VbRetryCancel

Retry

Cancel

Retry

and

buttons

and

No

Ignore

Cancel

buttons

buttons

buttons

Cancel

buttons

Symbols Available
The four available symbols are:

Symbol

What it means

VbCritical

Critical message

VbQuestion

Question

VbExclamation

Exclamation mark

VbInformation

Information sign

What it looks like

Named Arguments
If you have the time and energy, you can use named rather than positional arguments. The two
subroutines below have identical effect, but the one on the right is much easier to read:

It is up to you to remember which


argument is which for this subroutine

Wise
Wise Owls
Owls
Hint
Hint

Copyright 2016

Here it is obvious which argument is which, and we


could miss some out or change the order

One big advantage of named arguments you can put them in any order,
and dont need to include comma placeholders if you miss some
arguments out.

Page 28

Chapter 4 - Talking to the User

4.3

Finding Out Which Button Was Chosen

You can also use MsgBox as a function to control which buttons a user sees, and to determine
which button a user pressed:

This message box appears:


With
buttons
With the second button selected by default
With a question mark symbol

The Possible Buttons


You can test the result of the call to the MsgBox function against either the reserved Visual
Basic constant (eg vbOK) or the value (eg 1) they are both the same.

Copyright 2016

User chooses button

Value returned

Actual number

OK

vbOK

Cancel

vbCancel

Abort

vbAbort

Retry

vbRetry

Ignore

vbIgnore

Yes

vbYes

No

vbNo

Page 29

Chapter 4 - Talking to the User

4.4

Asking for Information

You can use the InputBox function to ask a user to type something in and then store it:
VariableName = InputBox(Question on input box, Title of input box, Default message)
In this example, we ask the user to type in the
name of a Muppet and then find this on the
worksheet shown on the right.
If you were to type in Miss Piggy, for example, the
macro would display the corresponding colour (Pink).

This is the dialog box we will display:


The Title of the input box (if
you miss this out, it will just
say Microsoft Excel).

The Default for the


input box is the
message which first
appears.

The question asked by the


input box (called the Prompt
by Microsoft).

Here is what our macro looks like:


This macro displays a message box asking
the user to type in the name of a muppet:

then shows the colour of


the muppet entered:

Wise
Wise Owls
Owls
Hint
Hint

Copyright 2016

You can not customise input boxes. So if you decide that you need an
input box with a pink background, say, you will need to draw and use a
UserForm.

Page 30

Anda mungkin juga menyukai