是“价值"吗?实际上是 Range 对象的默认属性? [英] Is "Value" actually the default property of the Range object?

查看:37
本文介绍了是“价值"吗?实际上是 Range 对象的默认属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开始之前,我想说我意识到你永远不应该依赖默认属性,我也不会,但这很奇怪.我一直读到 valueRange 对象的默认属性,因此为什么会这样:

Range("A1") = 2

但是,,_Default()_Item() 不完全相等.

Before I begin I want to say that I realize you should never depend on default properties and I won't, but this is curious. I've always read that value is the default property for the Range Object, hence why this works:

Range("A1") = 2

However, this page claims that item is the default property of Range.

Additionally, this sub I made would suggest that cells is the default property instead:

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

解决方案

The default member of the Range class is called _Default and is hidden. When you enable the "Show Hidden Members" feature in the Object Browser you can see it:

It has the exact same signature as the .Item property, so one of them is arguably an alias for the other.(*)

In any case, Range also implements the collection interface. As such, it can be used in a For Each loop — and when you do that, the loop will call .Item with each iteration and assign the current item to the loop variable.

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.

这篇关于是“价值"吗?实际上是 Range 对象的默认属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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