Covert string range to number range via VBA

Hi!

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!

Thanks!
monboisAsked:
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
0
 
Patrick MatthewsCommented:
Why not just force the string to a number in the VLOOKUP expression?  E.g.,

=VLOOKUP(VALUE(A2),LookupRange,7,FALSE)

You might also want to see this article:

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/A_2637-Six-Reasons-Why-Your-VLOOKUP-or-HLOOKUP-Formula-Does-Not-Work.html
0
 
AlanConsultantCommented:
Hi,

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):

=VLOOKUP(Value(A1),Table,OffsetValue)

HTH,

Alan.
0
Cloud Class® Course: C++ 11 Fundamentals

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

 
IndiaRahulCommented:
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.
0
 
SiddharthRoutCommented:
monbois,

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.

HTH

Sid
Sample.JPG
0
 
monboisAuthor Commented:
This is EXACTLY what I need! Thank you sooooo much!!!
0
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.