Val (function)

Syntax Val (string_expression)
Description Converts a given string expression to a number.
Comments The number parameter can contain any of the following:
  • Leading minus sign (for nonhex or octal numbers only)
  • Hexadecimal number in the format &Hhexdigits
  • Octal number in the format &Ooctaldigits
  • Floating-point number, which can contain a decimal point and an optional exponent
Spaces, tabs, and line feeds are ignored. If number does not contain a number, then 0 is returned.
The Val function continues to read characters from the string up to the first nonnumeric character. The Val function always returns a double-precision floating-point value. This value is forced to the data type of the assigned variable.
Example This example inputs a number string from an InputBox and converts it to a number variable.
Sub Main()
????a$ = InputBox("Enter anything containing a number","Enter Number")
????b# = Val(a$)
????MsgBox "The value is: " & b#
End Sub
'The following table shows valid strings and their numeric equivalents:
'????"1 2 3"????????123
'????"12.3"????????????????????????12.3
'????"&HFFFF"????????????????????-1
'????"&O77"????????????????????????63
'????"12.345E-02"????????????.12345
See Also CDbl (function); Str, Str$ (functions).