Val Function

Returns the numbers contained in a string as a numeric value of appropriate type.

Syntax

Val([string])

The optional string argument is any valid string expression. If this argument is omitted, is a non-initialized variable, or Null, the function returns 0.

Remarks

The Val function stops reading the string at the first character it can't recognize as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized. However, the function recognizes the radix prefixes &O (for octal) and &H (for hexadecimal). Blanks, tabs, and linefeed characters are stripped from the argument.

The following returns the value 1835:

Val(" 1 835 dollars 28 cents")

In the code below, Val returns the decimal value -1 for the hexadecimal value shown:

Val("&HFFFF")

Use the IsDate function to determine if date can be converted to a date or time. CVDate recognizes date literals and time literals as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a time of day, starting at midnight.

Note The Val function recognizes only the period (.) as a valid decimal separator. When different decimal separators are used, as in international applications, use CDbl instead to convert a string to a number.

Example

Dim MyValue
MyValue = Val("2457")   ' Returns 2457.
MyValue = Val(" 2 45 7")   ' Returns 2457.
MyValue = Val("24 and 57")   ' Returns 24.

 

See Also

Str Function, Type Conversion Functions