Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel vba - convert string to number

So, I used the left function to take the first 4 characters of a string and I need to run a vlookup with it, but it won't find the match because it's looking through numbers.

I want to do this in a macro, so I'm not sure about the syntax. Can someone help?

like image 544
user960358 Avatar asked Sep 30 '11 20:09

user960358


People also ask

How do I convert a string to a number in VBA?

In order to convert a string to integer in VBA, first, we need to check whether the string can be converted. If not it will return a different string. In order to check whether the string is a numerical value, we will use the ISNUMERIC Function. Then we will use Cint to convert the string to an integer.

How do I convert a string to a number in Excel?

Select the cells that have numbers stored as text. On the Home tab, click Paste > Paste Special. Click Multiply, and then click OK. Excel multiplies each cell by 1, and in doing so, converts the text to numbers.


2 Answers

use the val() function

like image 83
malificent Avatar answered Sep 20 '22 21:09

malificent


If, for example, x = 5 and is stored as string, you can also just:

x = x + 0

and the new x would be stored as a numeric value.

like image 25
asalamanca Avatar answered Sep 19 '22 21:09

asalamanca