VBA VAL Function (Syntax + Example)

Last Updated: June 22, 2023
puneet-gogia-excel-champs

- Written by Puneet

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 that is stored as a string and returns it as a number.

Syntax

Val(String)

Arguments

  • String: A string that you want to convert into a number.

Example

To practically understand how to use the VBA VAL function, you need to go through the below example where we have written a vba code by using it:

example-vba-val-function
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 cells A1 and A2:

  1. In cell A1 we have the value starting with a number and it has extracted that value in cell B1.
  2. But in cell A2, the value is starting with a character that can’t be recognized as a number so it has ignored all the characters after that.

Notes

  • 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 as part of numeric values.