Microsoft Office Tutorials and References
In Depth Information
Input Boxes
Case vbNo
MsgBox _
“No problem, this worksheet will not be copied.”, _
vbInformation, _
“You clicked No.”
Exit Sub
Case vbYes
MsgBox _
“Great - - click OK to run the macro.”, _
vbInformation, _
“Thanks for confirming.”
ActiveSheet.Copy Before:=Sheets(“Sheet1”)
End Select
End Sub
As you look at the MsgBox line, note that the message box arguments are contained within
parentheses. A message box has two mandatory arguments: the prompt , which is the text you place in
the body of the message box, and the button configuration. Other combinations of buttons include
OKCancel , YesNoCancel , and AbortRetryIgnore . The title of the message box is optional, but I
always enter it to offer a more customized experience for the user.
In the Try It section at the end of Lesson 7, you learned about VBA’s IntelliSense
feature. I recommend you activate IntelliSense if you have not already done so,
because when composing message boxes, you’ll be reminded of the available
arguments and their proper syntax while you are writing your code.
input Boxes
When you need a piece of specific information from the user, such as a text string or a number, an
InputBox was made for the job. An InputBox looks like a distant cousin of a message box, with the
prompted text that tells the user what to do, OK and Cancel buttons (which cannot be reconfigured
as a message box’s buttons can), and an optional title argument.
An InputBox requires a prompt argument, and it provides a field wherein the user would enter the
kind of information as needed for the macro to continue. The entry would return a String type
variable. If no entry is made, that is, the text field is left empty, the InputBox would return a null
string, which is usually regarded by VBA the same as if
the user clicked the Cancel button.
The following example uses an InputBox to ask the user
to enter a number to represent how many rows will be
inserted below the active cell’s row. Figure 8-2 shows what
the InputBox looks like for this macro.
Sub InsertRows()
‘Declare the string variable for the InputBox entry.
Dim CountInsertRows As String
‘Define the String variable as the InputBox entry.
figurE 8-2
 
Search JabSto ::




Custom Search