What is VBA VAL Function
The VBA VAL function is listed under the data type conversion category of VBA functions. When you use it in a VBA code, it converts a string into a number. In simple words, it takes a number which is stored as a string and returns it as a number.
how to use it
To use VBA's VAL function you need to understand its syntax and arguments:
- String: A string that you want to convert into a number.
Example to use VAL Function in VBA
To practically understand how to use VBA VAL function, you need to go through the below example where we have written a vba code by using it:
Sub example_VAL() Range("B1").Value = Val(Range("A1")) Range("B2").Value = Val(Range("A2")) End Sub
In the above code, we have used VAL to get the numeric value from the cell A1 and A2:
- In cell A1 we have the value starting with a number and it has extracted that value in the cell B1.
- But in the cell A2, the value is starting with a character which can’t be recognized as a number so it has ignored all the character after that.
Below are some important points which you need to take care while using VAL function in VBA.
- It stops reading the string at the first character it can't recognize as a number.
- It recognizes a period (.) as a valid decimal separator.
- It recognizes symbols and characters part of numeric values.
About the Author
Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can ﬁnd him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.