See Also Example
Returns the numeric value of a string of characters.
The argument stringexpression is a sequence of characters that can be interpreted as a numeric value. The Val function stops reading the string at the first character that it cannot recognize as part of a number. Val also strips blanks, tabs, and line feeds from the argument string. For example, the following returns the value 1615198:
Val(" 1615 198th Street N.E.")
Symbols and characters often considered part of a numeric value, such as the dollar sign and commas, are not recognized by Val as numeric. The Val function does recognize the radix prefixes &O (for octal) and &H (for hexadecimal). In the code below, Val returns the decimal value -1 for the hexadecimal value shown:
The Val function always returns a Double. If the result of the Val function is assigned to a variable, the Double returned by Val is forced into the data type of the variable.
Tip If the string expression To convert a numeric value to a String or Variant , you can use the Format, CStr, or CVar function. you want to convert to numbers contains only numbers, consider using one of the numeric data type conversion functions (CCur, CDbl, CInt, CLng, CSng, or CVar) to perform the conversion.
Note The usefulness of the Val function is all but eliminated if you use the Variant data type for your variables. This is because conversion back and forth between strings and numbers is accomplished automatically.
Data Type Conversion Functions
Format, Format$ Function
Str, Str$ Function
Val Function Example
The example uses the Val function to determine if the first characters in a string are a number. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim Msg ' Declare variables.
Dim Number As Double
Number = Val(InputBox$("Enter a number")) ' Get user input.
Msg = "The number you entered is:" & Number
MsgBox Msg ' Display message.