Page 160 - Excel Progamming Weekend Crash Course
P. 160
h540629 ch10.qxd 9/2/03 9:34 AM Page 135
Session 10 — Using Ranges and Selections 135
Display a Range to the User
To display a range to the user, use the Range object’s Select method. The
range will be selected in the worksheet, just as if the user had selected it.
This lets the user verify that the range is correct. Here’s the code required:
Do
Range1.Select
reply = MsgBox(“Is this the correct range?”, vbYesNo)
If replay = vbYes Then
‘ Code to process Range1 goes here.
Else
‘ Code to change Range1 goes here.
End If
Loop While reply <> vbYes
Remember that to let the user verify a range, the worksheet must be visible.
The Range.Activate method selects the one cell at the top left corner of
the range, regardless of how large the range is. The Range.Select method
Tip selects the entire range. For a range that points to a single cell, the Select
and Activate methods are equivalent.
REVIEW
The Range object is essential for many aspects of Excel programming.
A Range can identify anything from a single cell up to an entire worksheet.
Your program can have as many Range objects defined at the same time as are
needed.
You can assign a name to a Range object and then use the name to refer to it.
A Range can be defined in terms of absolute row and column locations, or relative
to the location of another range.
The Range.Value property is used to write data to, and read data from, cells in a
range.
The Selection property returns a Range object that represents the current selection.
QUIZ YOURSELF
1. How many ranges can you define in a given worksheet? (See the section “The
Range Object.”)
2. If you assign a name to a range, is the name remembered the next time the work-
book is opened? (See the section “Naming Ranges.”)