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

后端 未结 6 1890
孤独总比滥情好
孤独总比滥情好 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:41

    .Text is the formatted cell's displayed value; .Value is the value of the cell possibly augmented with date or currency indicators; .Value2 is the raw underlying value stripped of any extraneous information.

    range("A1") = Date
    range("A1").numberformat = "yyyy-mm-dd"
    debug.print range("A1").text
    debug.print range("A1").value
    debug.print range("A1").value2
    
    'results from Immediate window
    2018-06-14
    6/14/2018 
    43265 
    
    range("A1") = "abc"
    range("A1").numberformat = "_(_(_(@"
    debug.print range("A1").text
    debug.print range("A1").value
    debug.print range("A1").value2
    
    'results from Immediate window
       abc
    abc
    abc
    
    range("A1") = 12
    range("A1").numberformat = "0 \m\m"
    debug.print range("A1").text
    debug.print range("A1").value
    debug.print range("A1").value2
    
    'results from Immediate window
    12 mm
    12
    12
    

    If you are processing the cell's value then reading the raw .Value2 is marginally faster than .Value or .Text. If you are locating errors then .Text will return something like #N/A as text and can be compared to a string while .Value and .Value2 will choke comparing their returned value to a string. If you have some custom cell formatting applied to your data then .Text may be the better choice when building a report.

提交回复
热议问题