Covert string range to number range via VBA

Posted on 2011-10-12
Last Modified: 2012-05-12

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!

Question by:monbois
    LVL 92

    Expert Comment

    by:Patrick Matthews
    Why not just force the string to a number in the VLOOKUP expression?  E.g.,


    You might also want to see this article:
    LVL 12

    Expert Comment


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



    LVL 80

    Accepted Solution

    <<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
    LVL 3

    Expert Comment

    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.
    LVL 30

    Expert Comment


    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.



    Author Closing Comment

    This is EXACTLY what I need! Thank you sooooo much!!!

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
    This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
    This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
    This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now