Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Covert string range to number range via VBA

Posted on 2011-10-12
6
Medium Priority
?
482 Views
Last Modified: 2012-05-12
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!
0
Comment
Question by:monbois
6 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 36959527
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
 
LVL 21

Expert Comment

by:Alan
ID: 36959528
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
 
LVL 81

Accepted Solution

by:
byundt earned 1000 total points
ID: 36959726
<<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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:IndiaRahul
ID: 36960363
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
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 36960435
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
 

Author Closing Comment

by:monbois
ID: 36961898
This is EXACTLY what I need! Thank you sooooo much!!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question