VBA Val Function

This tutorial will demonstrate how to use the Val VBA function.

Val Function

VBA Val Convert Expression to Number

The VBA Val function converts an expression to a number. Val function will return 0 if the first character is non-numerical. It ignores spaces and it will stop evaluating as soon as it finds a character that has no numerical meaning.

Sub ValExample_1()
MsgBox Val("Hello World!")  'Result is: 0
MsgBox Val("1Hello World!") 'Result is: 1
MsgBox Val("Hello World!1") 'Result is: 0

MsgBox Val("1600 Pennsylvania Avenue NW, Washington, DC 20500, United States")
'Result is: 1600

MsgBox Val("1 2  3   4  5")  'Result is: 12345
MsgBox Val("1 2A  3   4  5") 'Result is: 12

MsgBox Val("12.34 5")  'Result is: 12.345
MsgBox Val("67,8 9  ") 'Result is: 67

MsgBox Val("27 28 High Street")      'Result is: 2728
MsgBox Val("    27  28 High Street") 'Result is: 2728
MsgBox Val("27 - 28 High Street")    'Result is: 27

End Sub