Val Function

Named Arguments

No

Syntax

Val(string)

string

Use: Required

Data Type: String

Any string representation of a number.

Return Value

A numeric data type able to hold the number contained in string.

Description

Converts a string representation of a number into a numeric data type.

Rules at a Glance

  • The Val function starts reading the string with the leftmost character and stops at the first character it doesn't recognize as being part of a valid number. For example, the statement:

    iNumber = Val("1A1")

    returns 1.

  • &O and &H (the octal and hexadecimal prefixes) are recognized by the Val function.

  • Currency symbols such as $ and £, and delimiters such as commas aren't recognized as numbers by the Val function.

  • The Val function recognizes only the period (.) as a decimal delimiter.

  • Prior to processing string, Val removes spaces, tabs, and linefeed characters.

Programming Tips and Gotchas

If you are developing an international application, you should use the more modern, internationally aware CDbl function to convert strings to numbers, since CDbl can recognize decimal separators.

See Also

CCur Function, CDbl Function, CDec Function, CInt Function, CLng Function, CSng Function

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.