Covert string range to number range via VBA


I'm having trouble with a VLookup in my Excel spreadsheet because the numbers remain as strings even after I run the range through a number formatting sub-routine. I keep finding ways to convert a single string value ("123") to a long integet by assigning it to variable such as:

longVar = Clng("MyString")

But this doesn't work with a range. Any help would be greatly appreciated!

Who is Participating?
byundtConnect With a Mentor Commented:
<<even after I run the range through a number formatting sub-routine>>

Changing the format of a cell does not change the value. That's why you are having difficulty.

If you want to convert a worksheet range from text that looks like numbers into real numbers, the easy way is to copy a blank cell and then do a Paste Special...Add on the target range. Alpha text and blanks are ignored, but text that looks like numbers is converted (you may need to reformat after doing this step). Here is the VBA equivalent:

Cells(Rows.Count, Columns.Count).Copy
MyRange.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd
Patrick MatthewsCommented:
Why not just force the string to a number in the VLOOKUP expression?  E.g.,


You might also want to see this article:

Not sure if you have some other reason for wanting to do this via VBA, but I would just convert the string using a formula.

If you have A1 = "123" (string), and you have a table with numerics that you want to lookup in, then use the following (Table is a range where the table is, and OffsetValue is the offset within the VLOOKUP table that you want to return):



Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

you dont need macro to convert these strings to number. Just write 1 in one of the empty cells, copy and select the range where the string to be converted are present. Then Paste Special ->Multiply and ok.

Patrick and Brad have already given you great methods to overcome the issue that you are facing. Here is a third way which doesn't involve formulas or VBA.

I can give you exact directions if you can tell me which office version you are using. The gist is to set the error checking for numbers stored as text. When you do that you get a small little green triangle on the top left corner of the cell. See the attached image. All you need to do is select the complete range and click on the "Exclamation mark" and then select "Convert To Number" And you are done :) Now your vlookup formula will work as expected.


monboisAuthor Commented:
This is EXACTLY what I need! Thank you sooooo much!!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.