Is the value "Value" the default property of the Range object?

Before starting, I want to say that I understand that you should never depend on the default properties, and I will not, but it is curious. I always read that value is the default property for a Range object, so why this works:

 Range("A1") = 2 

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

In addition, this suggestion I made suggests that cells is the default property:

 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 
+5
source share
2 answers

The default element of the Range class is called _Default and is hidden. When you enable the "Show hidden members" function in the Object Explorer, you can see it:

VBA Object Browser

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

In any case, Range also implements the collection interface. That way, it can be used in a For Each loop - and when you do this, the loop will call .Item with each iteration and assign the current element to the loop variable.

When used outside the enumeration, for example, with Debug.Print , then .Value will be .Value , but I can not explain why. Maybe someone else can come up with a hint. (*)


(*) Since @GSerg points to in the comments, _Default() and _Item() not quite equal.

+6
source

Yes, it seems that Value is the default value for Range.

I put a very large number in cell A1.

 Sub Test() Debug.Print "Range default = " & Range("A1") & vbCrLf _ & "Range Text = " & Range("A1").Text & vbCrLf _ & "Range Value = " & Range("A1").Value & vbCrLf _ & "Range Value2 = " & Range("A1").Value2 End Sub Results in Range default = 3.24643541346456E+28 Range Text = 3.25E+28 Range Value = 3.24643541346456E+28 Range Value2 = 3.24643541346456E+28 

Note that the result of Range ("A1") is the same as range ("A1"). Value and range ("A1"). Value2

0
source

Source: https://habr.com/ru/post/1233179/


All Articles