VBA VAL Function (Syntax + Example)

HomeVBA TutorialTop VBA FunctionsVBA VAL Function (Syntax + Example)

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.

Syntax

Val(String)

Arguments

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

Example

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:

  1. In cell A1 we have the value starting with a number and it has extracted that value in the cell B1.
  2. 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.

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

Related Functions

CBOOL | CBYTE | CCUR | CDATE | CDBL | CDEC | CHR | CINT | CLNG | CSNG | CSTR | CVAR | FORMATCURRENCY | FORMATDATETIME | FORMATNUMBER | FORMATPERCENT | HEX | OCT | STR