Number to text (etc.)
The following example applies the
worksheet functions value
,
numberValue
,
n
,
t
and
text
to a number (
18.3
), a text that is numerical (
"18.3"
), a «normal» text (
"text"
) and the two
logical values true
and
false
:
option explicit
sub main() ' {
cells(2,1) = 18.3
cells(3,1) ="'18.3"
cells(4,1) ="text"
cells(5,1) = true
cells(6,1) = false
cells(1,2) = "value" : range(cells(2,2), cells(6,2)).formulaR1C1 = "=value(rc1)"
cells(1,3) = "numberValue" : range(cells(2,3), cells(6,3)).formulaR1C1 = "=numberValue(rc1)"
cells(1,4) = "n" : range(cells(2,4), cells(6,4)).formulaR1C1 = "=n(rc1)"
cells(1,5) = "t" : range(cells(2,5), cells(6,5)).formulaR1C1 = "=t(rc1)"
cells(1,6) = "text" : range(cells(2,6), cells(6,6)).formulaR1C1 = "=text(rc1, ""000.00"")"
range(cells(1,2), cells(1,6)).font.bold = true
activeSheet.usedRange.columns.autofit
end sub ' }
When this
VBA code is executed, it produces:
It demonstrates the following conversion rules:
Function | Return value |
value | A number if the argument can be converted to a number, else, #VALUE error |
numberValue | Similar to value , but locale-independent (no differences on my machine) |
n | If argument is a number: the number itself; if argument is boolean: 0 or 1 ; else 0 |
t | If argument is a text: the text itself; if argument is boolean: FALSE or TRUE ; else the empty string |
text | If argument is a text: the text itself; if argument is boolean: FALSE or TRUE ; if numeric: apply format string to number. |
complex
=complex(3,2)
evaluates to a textual representation of an imaginary number.
Because the data type of the result is
text, it cannot be used for further calculations.