在开始之前我想说,我意识到你永远不应该依赖默认属性,我也不会,但这很奇怪。我一直读到过value
是默认属性Range
对象,这就是为什么它有效:
Range("A1") = 2
然而,this页面声称item
是默认属性Range
.
此外,我制作的这个子集建议cells
是默认属性:
Sub defaultprop()
Dim c As Variant
For Each c In Range("A1:A2")
Debug.Print c.value
Next c
For Each c In Range("A1:A2").value
Debug.Print c
Next c
End Sub
默认成员Range
类称为_Default
并且被隐藏。当您在对象浏览器中启用“显示隐藏成员”功能时,您可以看到它:
![VBA Object Browser](https://i.stack.imgur.com/L4HU2.png)
It has the exact same signature as the .Item
property, so one of them is arguably an alias for the other.(*)
任何状况之下,Range
还实现了集合接口。因此,它可以用于For Each
循环——当你这样做时,循环将调用.Item
每次迭代并将当前项分配给循环变量。
When used outside an enumeration, for example with Debug.Print
, then .Value
will be used, but I can't really explain why. Maybe someone else can come up with a hint.(*)
(*) As @GSerg points out in the comments, _Default()
and _Item()
are not exactly equal.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)