Page 62 - Excel for Scientists and Engineers: Numerical Methods
P. 62
CHAPTER 2 FUNDAMENTALS OF PROGRAMMING WITH VBA 39
Figure 2-14. A Msgbox display.
The values of buttons are built-in constants-for example, the value 64 for
buttons can be replaced by the variable name vblnformation. The same result, a
dialog box with a Warning Query icon, with Yes, No and Cancel buttons and
with the No button as default, can be obtained by using the expression
vblnformation + vbYesNoCancel + vbDefaultButton2
in the MsgBox function instead of the value 323.
MsgBox Return Values
MsgBox can return a value that indicates which button was pressed. This
allows you to take different actions depending on whether the user pressed the
Yes, No or Cancel buttons, for example. To get the return value of the message
box, use an expression like
Buttonvalue = Msg Box (prompt-text, buttons, title-text, helpfile, context)
(Note that the arguments of MsgBox must be enclosed in parentheses in order
for it to return a value.)
The return values of the buttons are as follows: OK, 1; Cancel, 2; Abort, 3;
Retry, 4; Ignore, 5; Yes, 6; No, 7.
InputBox
The InputBox allows you to pause a macro and request input from the user.
There are both an InputBox function and an InputBox method.
The syntax of the InputBox function is
Input Box (prompt_ text, title- tex t, default, x-position, y-position, help file,
context)
where prompt_text and title-text are as in MsgBox. Default is the expression
displayed in the input box, as a string. The horizontal distance of the left edge of
the box from the left edge of the screen, and the vertical distance of the top edge
from the top of the screen are specified by xjosition and y-position,