Microsoft Office Tutorials and References
In Depth Information
Controlling Code Execution
Case 1
MsgBox “One cell is selected”
Case Else
MsgBox Selection.Rows.Count & “ rows”
End Select
Case “Nothing”
MsgBox “Nothing is selected”
Case Else
MsgBox “Something other than a range”
End Select
End Sub
This procedure also demonstrates the use of Case Else , a catch-all case. You can nest Select
Case constructs as deeply as you need, but make sure that each Select Case statement has a
corresponding End Select statement.
This procedure demonstrates the value of using indentation in your code to clarify the structure.
For example, take a look at the same procedure without the indentations:
Sub SelectionType()
Select Case TypeName(Selection)
Case “Range”
Select Case Selection.Count
Case 1
MsgBox “One cell is selected”
Case Else
MsgBox Selection.Rows.Count & “ rows”
End Select
Case “Nothing”
MsgBox “Nothing is selected”
Case Else
MsgBox “Something other than a range”
End Select
End Sub
Fairly incomprehensible, eh?
Looping blocks of instructions
Looping is the process of repeating a block of instructions. You might know the number of times
to loop, or the number may be determined by the values of variables in your program.
The following code, which enters consecutive numbers into a range, demonstrates what I call a
bad loop. The procedure uses two variables to store a starting value ( StartVal ) and the total
number of cells to fill ( NumToFill ). This loop uses the GoTo statement to control the flow. If the
Cnt variable, which keeps track of how many cells are filled, is less than the value of
NumToFill , the program control loops back to DoAnother .
 
Search JabSto ::




Custom Search