开发者

What is the difference between Range.Columns and Range.EntireColumn

开发者 https://www.devze.com 2023-01-03 22:43 出处:网络
Dim r as Range Set r = Range(\"C2:D3\") Dim r1 as Range, r2 a开发者_运维知识库s Range Set r1 = r.EntireColumn
Dim r as Range
Set r = Range("C2:D3")

Dim r1 as Range, r2 a开发者_运维知识库s 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?


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消