tags:

views:

83

answers:

1
Dim r as Range
Set r = Range("C2:D3")

Dim r1 as Range, r2 as Range
Set r1 = r.EntireColumn
Set r2 = r.Columns

Won't both ranges represent the range "C:D"? What is the difference between the two?

+1  A: 

No, EntireColumn represents the range "C:D", Columns represents the columns of the cells in the range. If you want to see this in action, here's a small sub that shows this. Place non-zero values in the entire range C2:D3, then place some in C5 and D5. The values in C5 and D5 won't change with Columns (range1), now substitute EntireColumn (range2) and see what happens.

Sub Test()

Dim range1 As Range
Dim range2 As Range

    Set range1 = Range("C2:D3").Columns
    Set range2 = Range("C2:D3").EntireColumn

    range1.Value = 0

End Sub

Also, Columns is indexed, so you can reference the first column like:

r.Columns(1)
Lance Roberts
So Range("C2:D3") and Range("C2:D3").Columns represent the exact same set of cells? So a "Range" object can't simply be defined by the set of cells that it contains? (as Range("C2:D3") and Range("C2:D3").Columns cover the same set of cells but are different)
Laurent
There are many ways a Range can be defined. You can look at the details of the object in the code editor. You do this by putting the code in a sub, set a breakpoint at the end, then in the Watch window setting a watch for the Range. Then you can look at all the gory details.
Lance Roberts