Val function
Returns the numbers contained in a string as a numeric value of appropriate type.
Syntax
Val(string)
The required string argument is any valid string expression.
Remarks
The Val function stops reading the string at the first character that 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 1615198:
Val(" 1615 198th Street N.E.")
In the following code, Val returns the decimal value -1 for the hexadecimal value shown:
Val("&HFFFF")
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
This example uses the Val function to return the numbers contained in a string.
Dim MyValue
MyValue = Val("2457") ' Returns 2457.
MyValue = Val(" 2 45 7") ' Returns 2457.
MyValue = Val("24 and 57") ' Returns 24.
Note
The Val function recognizes deprecated data type suffixes prior to conversion and may result in a type mismatch error. For example, fifty percent represented as the string "50%" will convert as expected to 50 but Val("50.5%") will raise an error because the percentage symbol is seen as a suffix to declare the Data Type as an Integer, which it is not in this case. The full list of data type suffixes comprises Single ( ! ), Currency ( @ ), Double ( # ), String ( $ ), Integer ( % ), Long ( & ) and LongLong ( ^ ) for 64-bit hosts.
See also
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기