Range.Item和Range.Cells有什么区别? [英] What's the difference between Range.Item and Range.Cells?
问题描述
例如,在下面的代码中,Item
和Cells
可以互换使用:
For example, in the code below, Item
and Cells
can be used interchangeably:
Dim rRange As Range
Set rRange = ThisWorkbook.ActiveSheet.Range("A1")
With rRange
Debug.Print .Item(1, 1).Value ' Outputs value of "A1"
Debug.Print .Cells(1, 1).Value ' Outputs value of "A1"
Debug.Print .Item(2, 1).Value ' Outputs value of "A2"
Debug.Print .Cells(2, 1).Value ' Outputs value of "A2"
End With
在开发人员参考中,它们定义为:
In the developer reference, they are defined as:
返回一个Range对象,该对象表示相对于的偏移量的范围 指定范围.
Returns a Range object that represents a range at an offset to the specified range.
〜
返回一个Range对象,该对象代表指定范围内的单元格.
Returns a Range object that represents the cells in the specified range.
备注
由于Item属性是Range对象的默认属性,因此可以在Cells关键字之后立即指定行索引和列索引.
Because the Item property is the default property for the Range object, you can specify the row and column index immediately after the Cells keyword.
根据上述说法,是否表示Cells(1, 1)
实际上是Cells.Item(1, 1)
的缩写?因此Cells(1, 1)
实际上等于Item(1, 1)
吗?我想念什么?
From that remark, does it mean that Cells(1, 1)
is actually a short for Cells.Item(1, 1)
? Thus Cells(1, 1)
is actually equivalent to Item(1, 1)
? What am I missing?
推荐答案
了解这一点的最佳方法是通过以下示例
The best way to understand this is via the below example
当对范围使用.Item
和.Cells
时,是的,它们是相同的.例如
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
在上面,它们都描述了Range
In the above they both depict the address of the cell in that Range
单独使用Cells()
时,它们是不同的.
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
上面的
描述了Range
中单元格的地址,而Cells
描述了ActiveSheet
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
这篇关于Range.Item和Range.Cells有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!