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.
- String: A string that you want to convert into a number.
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.
- 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.