+ Reply to Thread
Results 1 to 5 of 5

Best way to compare values in a range?

  1. #1
    Registered User
    Join Date
    04-08-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    85

    Best way to compare values in a range?

    Using VBA, I want to import data from a text file into a worksheet, but only import a row of text if the three-letter code matches one I have in a named range. The data looks similar to this:

    ALF,D,06/01/2009,0.96,0.97,0.96,0.97,1007,0
    ALK,D,06/01/2009,0.34,0.36,0.34,0.345,4375,0
    ALL,D,06/01/2009,3.7,3.8,3.68,3.75,12381,0
    ALR,D,06/01/2009,1.155,1.18,1.155,1.17,388,0
    ALS,D,06/01/2009,3.39,3.39,3.23,3.31,2642,0
    ALT,D,06/01/2009,0.031,0.031,0.031,0.031,3460,0
    ALY,D,06/01/2009,0.1,0.105,0.1,0.105,460,0
    etc...

    and the named range similar to this:

    AAC
    AAF
    AAM
    AAR
    AAX
    ABB
    ABC
    ABP
    ABQ
    etc....

    I know how to open and import the data, it's just making the comparisons
    that I'm having trouble with at the moment . Any ideas, maybe an example of VBA code which does something similar please?
    Last edited by jp001; 08-08-2009 at 02:33 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Best way to compare values in a range?

    Hello jp001,

    The easiest and quickest method is to use an array. The drawback with a standard array is you can't easily check if an entry has already been made. You can get the best of both using a Dictionary Object. The name is really a misnomer. It is an associative array which has the capability of checking if a member already exists. Have you used this before?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-08-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Best way to compare values in a range?

    Thanks Leith, no I've haven't used that before. Would you be able
    to point me towards a code sample? Thanks!

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Best way to compare values in a range?

    Hello jp001,

    Here is a sample workbook using the Dictionary Object to compare the range names. I have a list of range names in column "M" on "Sheet1". The data is read in from a text file using the data you posted. The first field of the line data is tested using the DSO to see if the rnage name exists. If it does then it will copy the input fields to A1:H1 and go down one row for each match.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    04-08-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Best way to compare values in a range?

    Thanks for that Leith! (Been away for a few days and just saw your answer.) Actually I found a similar one with the search, but it's useful to see different ways of doing the same thing. Much appreciated!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1