totn Excel Functions

MS Excel: How to use the VAL Function (VBA)

This Excel tutorial explains how to use the Excel VAL function with syntax and examples.

Description

The Microsoft Excel VAL function accepts a string as input and returns the numbers found in that string.

TIP: The Val function stops reading the string at the first character it can't recognize as part of a number. This means that the Val( string ) function only seems to work when the number in that string is at *the beginning* of said string.

The VAL function is a built-in function in Excel that is categorized as a String/Text Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.

Syntax

The syntax for the VAL function in Microsoft Excel is:

Val( string )

Parameters or Arguments

string
A string expression that you wish to return the numbers found within.

Returns

The VAL function returns a numeric value.

Note

  • The VAL function will stop reading the string once it encounters the first non-numeric character. This does not include spaces.
  • If no numeric value is found at the start of the string, the Val function will return 0.

Applies To

  • Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

The VAL function can only be used in VBA code in Microsoft Excel.

Let's look at some Excel VAL function examples and explore how to use the VAL function in Excel VBA code:

Val("10 Main Street")
Result: 10

Val("34 10 Main Street")
Result: 3410

Val("  34 10 Main Street")
Result: 3410

Val("  34 - 10 Main Street")
Result: 34

Val("075")
Result: 75

Val("There are 5 houses")
Result: 0

For example:

Dim LNumber As Double

LNumber = Val("56.2 tables")

In this example, the variable called LNumber would now contain the value of 56.2.