gpt4 book ai didi

excel - Range.Item 和 Range.Cells 之间有什么区别?

转载 作者:行者123 更新时间:2023-12-01 19:49:57 30 4
gpt4 key购买 nike

例如,在下面的代码中,ItemCells 可以互换使用:

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
<小时/>

在开发人员引用中,它们定义为:

Range.Item Property (Excel)

Returns a Range object that represents a range at an offset to the specified range.

~

Range.Cells Property (Excel)

Returns a Range object that represents the cells in the specified range.

Remarks

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)?我错过了什么?

最佳答案

理解这一点的最佳方法是通过下面的示例

.Item.Cells 用于某个范围时,是的,它们是相同的。例如

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

在上面,它们都描述了该范围中单元格的地址

Cells() 独立于范围使用时,它们是不同的。

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

在上面的.Item中描述了该Range中单元格的地址,其中Cells描述了该Range中单元格的地址ActiveSheet

关于excel - Range.Item 和 Range.Cells 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25879619/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com