Return to VBA Code Examples

VBA Val Function

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on July 19, 2021

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

 

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! vba save as


Learn More!
vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples