为什么在链接时,Range.Address()会返回第一个单元格的地址? [英] Why does Range.Address() return the address of the first cell when chained?

查看:610
本文介绍了为什么在链接时,Range.Address()会返回第一个单元格的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为答案时,我偶然遇到意外的行为/ q / 27827384/3198973>这个问题。



链接在一起范围 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

Documentation here:

When applied to a Range object, the property is relative to the Range object.

For example, if the selection is cell C3, then Selection.Range("B1") returns cell D3 because it’s relative to the Range object returned by the Selection property. On the other hand, the code ActiveSheet.Range("B1") always returns cell B1.

这篇关于为什么在链接时,Range.Address()会返回第一个单元格的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆