Is “Value” actually the default property of the Range object?

前端 未结 2 959
孤城傲影
孤城傲影 2020-11-28 14:43

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 th

相关标签:
2条回答
  • 2020-11-28 15:32

    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:

    VBA Object Browser

    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.

    0 讨论(0)
  • 2020-11-28 15:34

    Yes, it appears that Value is default property of 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
    

    Notice that Range("A1")'s result is identical to Range("A1").Value and Range("A1").Value2

    0 讨论(0)
提交回复
热议问题