Val Function in Excel VBA
VAL stands for VALUE in VBA terminology. VBA VAL function converts the string containing number to an actual number. For example, if you supply the text string “1234 Global” it will return only the numerical part i.e. 1234.
Often times, when we download or get the data from web numbers, are usually stored as text values in a spreadsheet. Conversion of text to numbers is the hardest task if you are not aware of the correct function in excel. As a regular worksheet function, we have a function called VALUE which will convert all the string that represents numbers to exact numbers with a simple function in the worksheet. In this article, we will show how this can be achieved in VBA using the VAL function.
Syntax of VAL Function in VBA
Now take a look at the syntax of VAL function in VBA. It has only one argument i.e. String.
String: It is simply a string value we are trying to get the numerical part out of it.
So, the VAL function converts the supplied string to a numerical value.
For example, if the supplied string is “145 45 666 3” it will ignore the space characters and return the result as “145456663”.
4.6 (247 ratings)
Examples of VAL Function in Excel VBA
Below are the examples of VBA Val Function in Excel.
Example #1
Let’s try the first example with simple number i.e. “14 56 47”
Below code is for you.
Code:
Sub Val_Example1() Dim k As Variant k = Val("14 56 47") 'Convert the above as 145647 MsgBox k End Sub
When you run VBA code using F5 key or manually then, it will return the result as “145647” by ignoring all the space characters as shown in the below image.
Example #2
In this example, we will see what the result of the string is “+456”.
Code:
Sub Val_Example2() Dim k As Variant k = Val("+456") 'Convert the above as 456 MsgBox k End Sub
You can run this code manually or through F5 key to return the value as 456 by ignoring +456.
Example #3
Now let try the same number with a negative sign.
Code:
Sub Val_Example3() Dim k As Variant k = Val("-456") 'Convert the above as -456 MsgBox k End Sub
This code will return the value as -456 only because number with operator sign should be shown.
Example #4
Now let try this string “100 Kg”.
Code:
Sub Val_Example4() Dim k As Variant k = Val("100 KG") 'Ignores KG and returns only 100 MsgBox k End Sub
If you run this code manually or using F5 key then this above code ignores “KG” and returns only “100” in the VBA message box.
Example #5
Now try the date string i.e. “14-05-2018”.
Code:
Sub Val_Example5() Dim k As Variant k = Val("14-05-2019") 'Returns 14 as the result. MsgBox k End Sub
The above code returns 14 as the result because VBA VAL function can only fetch the numerical value until it finds any other than numerical character.
Example #6
Now try the string “7459Good456”.
Code:
Sub Val_Example6() Dim k As Variant k = Val("7459 Good 456") 'Returns 7459 as the result. MsgBox k End Sub
This will extract the numbers until it finds the non-numerical character i.e. result is 7459. Even though there are numerical values after the non-numerical value “Good”, it completely ignores numbers after that.
Example #7
Now try the string value “H 12456”.
Code:
Sub Val_Example7() Dim k As Variant k = Val("H 12456") 'Returns 0 as the result. MsgBox k End Sub
Run the above code using shortcut key F5 or manually then it returns the result as zero. Because the very first character of the string we have supplied is a non-numerical character so, the result is zero.
Example #8
Now try this string “24545 . 2”.
Code:
Sub Val_Example8() Dim k As Variant k = Val("24545 . 2") 'Returns 24545.2 as the result. MsgBox k End Sub
The code returns the result as 24545.2 because VBA VAL function considers the character dot (.) as the decimal character and returns the result accordingly.
Recommended Articles
This has been a guide to VBA Val Function. Here we learn how to use VAL Function in VBA along with practical examples and a downloadable template. Below you can find some useful excel VBA articles –
- VBA Regular Expression
- Examples to Change Font Color in VBA
- How to use CSTR in VBA?
- VBA Like Operator Examples
- Sleep Function in VBA
- VBA Pivot Table
- VBA Count Function
- Use VBA AutoFill
- Create Random Numbers in VBA
- Apply Break Points in VBA
- 3 Courses
- 12 Hands-on Projects
- 43+ Hours
- Full Lifetime Access
- Certificate of Completion