What's the difference between Range.Item and Range.Cells?

后端 未结 2 737
离开以前
离开以前 2020-12-17 21:50

For example, in the code below, Item and Cells can be used interchangeably:

Dim rRange As Range
Set rRange = ThisWorkbook.ActiveShe         


        
2条回答
  •  隐瞒了意图╮
    2020-12-17 22:06

    The best way to understand this is via the below example

    When .Item and .Cells are used with respect to a range then, YES, they are same. For example

    Sub Sample()
        Dim rRange As Range
        Set rRange = ThisWorkbook.ActiveSheet.Range("B1:C10")
    
        With rRange
            Debug.Print .Item(1, 3).Address '<~~ $D$1
            Debug.Print .Cells(1, 3).Address '<~~ $D$1
        End With
    End Sub
    

    In the above they both depict the address of the cell in that Range

    They are different when Cells() is used independently of a range.

    Sub Sample()
        Dim rRange As Range
        Set rRange = ThisWorkbook.ActiveSheet.Range("B1:C10")
    
        With rRange
            Debug.Print .Item(1, 3).Address  '<~~ $D$1
            '~~> DOT before Cells missing
            Debug.Print Cells(1, 3).Address  '<~~ $C$1
        End With
    End Sub
    

    In the above .Item depicts the address of the cell in that Range, where as Cells depicts the address of the cell in the ActiveSheet

提交回复
热议问题