为什么在链接时,Range.Address()会返回第一个单元格的地址? [英] Why does Range.Address() return the address of the first cell when chained?
问题描述
链接在一起范围
code>地址始终返回语句中第一个 Range
对象的地址。例如:
Public Sub Unexpected()
Debug.Print Range(B3)。地址
Debug.Print Range(B3)。Range(A1)。Address
End Sub
返回以下输出。
$ B $ 3
$ B $ 3
但我本来希望它返回最后一个的地址链接中的范围对象。
$ B $ 3
$ A $ 1
有人可以解释这个行为吗?
有关于使用范围的文档).Cells()
表示 Cells()
将返回给定的 Range()
相对于 Range()
的左上角单元格。测试这个理论与 Range()。Range()
给出:
Public Sub Unexpected()
Debug.Print范围(B1:C3)。范围(A1)。地址
'$ B $ 1
Debug.Print范围B1:C3)。范围(B1)。地址
'$ C $ 1
End Sub
文档这里:
当应用于
Range
对象时,属性相对于Range
对象。例如,如果选择是单元格C3
,则Selection.Range( B1)
返回单元格D3
,因为它与Range
code>选择属性。另一方面,代码ActiveSheet.Range(B1)
总是返回单元格B1
。
I stumbled across some unexpected behavior when writing an answer for this question.
When chaining together Range
calls, Address
always returns the address of the very first Range
object in the statement. For example:
Public Sub Unexpected()
Debug.Print Range("B3").Address
Debug.Print Range("B3").Range("A1").Address
End Sub
Returns the following output.
$B$3
$B$3
But I would have expected it to return the Address of the last Range object in the chain.
$B$3
$A$1
Can anyone explain this behavior? Preferably with a quote of and link to the appropriate documentation.
There is documentation on using Range().Cells()
which indicates that Cells()
will return the location within the given Range()
relative to the top left cell of the Range()
. Testing this theory with Range().Range()
gives:
Public Sub Unexpected()
Debug.Print Range("B1:C3").Range("A1").Address
'$B$1
Debug.Print Range("B1:C3").Range("B1").Address
'$C$1
End Sub
When applied to a
Range
object, the property is relative to theRange
object.For example, if the selection is cell
C3
, thenSelection.Range("B1")
returns cellD3
because it’s relative to theRange
object returned by theSelection
property. On the other hand, the codeActiveSheet.Range("B1")
always returns cellB1
.
这篇关于为什么在链接时,Range.Address()会返回第一个单元格的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!