the
Select Case
statement. Notice how this statement uses
Case
clauses
to determine which action to take. You can provide a single value, a range of
values, or a list of values separated as commas. The final step is to place the
Output
value in the worksheet.
Using
the Case Else clause
A
Select Case
statement should normally contain the optional
Case Else
clause to ensure that you handle all cases, even those that you don’t expect
when you write the program. Adding this clause requires little time and adds
an important error-trapping feature to your program.
You could easily change
the program from the previous section to include a
Select Case
statement
like the one shown in
Listing 5-6. (You can find the source code for this exam-
ple on the Dummies.com site at
http://www.dummies.com/go/vbafd5e
.)
Listing 5-6
Handling Unforeseen Decisions with Case Else
‘ Select a choice of storage room based in the bin.
Select Case BinValue
Case 1
Output = 1
Case 2
Output = 2
Case 3 To 4
Output = 1
Case 5 To 6
Output = 3
Case Else
‘ Tell the user to select only one cell.
MsgBox “Provide a Bin Number between 1 and 6”, _
vbExclamation Or vbOKOnly, _
“Bin Number Input Error”
‘ Exit the Sub without further processing.
End
End Select
The older version of the code could cause problems when you type a non-
existent bin number. This version provides an error
message telling you what
range of numbers to use. It exits before the code has a chance to create an
error condition.
123
Chapter 5: Creating Structured Programs
10_046500 ch05.qxp 12/5/06 5:35 PM Page 123
Performing a Task More than
Once by Using Loops
Many tasks that you perform require more than one check, change,
or data
manipulation. You don’t change just one entry in a worksheet; you change all
the affected entries. Likewise, you don’t change just one word in a document;
you might change all occurrences based on certain criteria.
Databases
require multiple changes for almost any task.
Loops
provide a method for performing tasks more than one time. You can
use loops to save code-writing time. Simply write the code to perform the
repetitive task once and then tell VBA to perform the task multiple times.
When using loops, you decide how the code determines when to stop. You
can tell the loop to execute a specific number of times or to continue execut-
ing until the program meets a certain condition.
Using the Do While...Loop statement
A
Do While...Loop
statement keeps performing a task until a certain con-
dition is true. The loop checks the expression first and then executes the
code within the structure if the expression is true. You use this loop to per-
form processing zero or more times. A
Do While...Loop
works especially
well if you can’t determine the number of times
that the loop should execute
when you design your program.
One example of a file that could require zero or more changes is a Word docu-
ment. You might need to format certain words in a specific way for each file,
but you have no idea which words you’ll use or whether you’ll use them at
all. The code in Listing 5-7 shows a technique for checking specific words and
formatting them. You could easily adapt this
program to meet your format-
ting needs. (You can find the source code for this example on the
Dummies.com site at
http://www.dummies.com/go/vbafd5e
.)
Do'stlaringiz bilan baham: