What is the difference between .text, .value, and .value2?

后端 未结 6 1888
孤独总比滥情好
孤独总比滥情好 2020-11-22 00:07

What is the difference between .text, .value, and .value2? Such as when should target.text, target.value, and target.value2 be used?

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 00:38

    target.Value will give you a Variant type

    target.Value2 will give you a Variant type as well but a Date is coerced to a Double

    target.Text attempts to coerce to a String and will fail if the underlying Variant is not coercable to a String type

    The safest thing to do is something like

    Dim v As Variant
    v = target.Value 'but if you don't want to handle date types use Value2
    

    And check the type of the variant using VBA.VarType(v) before you attempt an explicit coercion.

提交回复
热议问题