Page 157 - Excel Progamming Weekend Crash Course
P. 157

h540629 ch10.qxd  9/2/03  9:34 AM  Page 132




                132                                                         Saturday Morning


               Listing 10-3                                                     Continued
                  If r1 Is Nothing Then
                      Debug.Print “Please select some cells first.”
                      Exit Sub
                  End If

                  x = r1.Value
                  If IsArray(x) Then
                      Debug.Print “You selected a multiple cell range.”
                      For i = 1 To UBound(x, 1)
                          For j = 1 To UBound(x, 2)
                              Debug.Print “Cell “ & j & “,” & i; “ contains “ & x(i, j)
                          Next j
                      Next i
                  Else
                      Debug.Print “You selected a single cell.”
                      Debug.Print “Its value is “ & x
                  End If

                  End Sub
                  Sample program output is shown in Figure 10-6. This was generated using the range from
               Figure 10-5.













                    Figure 10-6 Sample output from the program in Listing 10-3



               Naming Ranges
               Any defined range can be assigned a name. One way to do this is by using the Range
               object’s Name property:
                  SomeRange.Name = “Sales Total”

                  The name that you assign must be unique within the workbook (not assigned to another
               range). After a range has been named in this way, you can access the range from the work-
               book’s Range collection using the following syntax:

                  Range(RangeName)
   152   153   154   155   156   157   158   159   160   161   162