+ Reply to Thread
Results 1 to 25 of 25

Comparing to Arrays and Sorting

  1. #1
    Registered User
    Join Date
    01-02-2013
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2010
    Posts
    25

    Comparing to Arrays and Sorting

    I'm hoping to compare a list of customers with an existing list. My thought would be to first setup an array of strings so that I could check each name and determine how many letters off a name might be from an existing name (in case there is a typo, name cut off short in raw data, etc.).

    I have labeled the worksheet "Data" with all the customer names from the raw data, and the customers are located in column A. The EXISTING customer list is in worksheet "Monthly Sales" and also in column A. It'd be nice to have a separate list then created of the names that don't match the existing customers so that the user could decide if this was a typing error or they are in fact not on the list of existing customers.

    I started with a simple loop but have not gotten very far as I'm not sure how to compare the two lists properly:

    Sub Sort()

    Dim myCustomerArray(50) As String
    Dim counter As Integer
    Dim i As Integer
    Set i = 1

    For counter = 2 To 33
    If Worksheets("Data").Cells(counter, 8) = "US40" Then
    myCustomerArray(i) = Worksheets("Data").Cells(counter, 1)
    i = i + 1
    MsgBox myCustomerArray(counter)
    End If
    Next
    End Sub

    This gets the raw data customer list, but how would i compare this array with another existing array and remove matches/sort alphabetically?

  2. #2
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Comparing to Arrays and Sorting

    Do you know any SQL? If so then comparing the lists is a trivial exercise using different JOINs.

    If you don't then I would recommend the following approach:
    1. Sort both lists alphabetically;
    2. Remove any duplicate values in a list so that each list contains only unique values;
    2. Use a Nested Loop model to identify matches and non-matches.

    The Nested Loop method is:
    1. start with the first entry in the first list and the first entry in the second list;
    2. compare the two values;
    3. if matched then store the value in a third list ("matched");
    4. if not matched then look at the next entry in the second list and do this until either you find a match (add to "matched" list) or you don't (add to a fourth list: "unmatched");
    5. move on to the next entry in the first list and repeat the above until you've compared everything.

    Again, SQL is much simpler (and faster) for this kind of work.
    Last edited by PingPing; 01-08-2013 at 10:37 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Comparing to Arrays and Sorting

    Use a collection with the data as the key, then you can trap the error of adding a duplicate key, i.e. a match.

    Look at help for collections.

  4. #4
    Registered User
    Join Date
    01-02-2013
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by nathansav View Post
    Use a collection with the data as the key, then you can trap the error of adding a duplicate key, i.e. a match.

    Look at help for collections.
    Do you mean in Excel itself?

  5. #5
    Registered User
    Join Date
    01-02-2013
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by PingPing View Post
    Do you know any SQL? If so then comparing the lists is a trivial exercise using different JOINs.

    If you don't then I would recommend the following approach:
    1. Sort both lists alphabetically;
    2. Remove any duplicate values in a list so that each list contains only unique values;
    2. Use a Nested Loop model to identify matches and non-matches.

    The Nested Loop method is:
    1. start with the first entry in the first list and the first entry in the second list;
    2. compare the two values;
    3. if matched then store the value in a third list ("matched");
    4. if not matched then look at the next entry in the second list and do this until either you find a match (add to "matched" list) or you don't (add to a fourth list: "unmatched");
    5. move on to the next entry in the first list and repeat the above until you've compared everything.

    Again, SQL is much simpler (and faster) for this kind of work.
    I don't know SQL unfortunately, but how might one go about using it if I'm new to it?

    Thanks for the response.

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Comparing to Arrays and Sorting

    No, collections in VBA, they are like arrays, but you can give them Keys to stop duplicates. So you could have a collection called colFormatting, and have keys for Bold, Underline, Color etc, then you can refer to them as font.bold = colFormatting("Bold").value

    or something like that, not in Excel at the minute to do.

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Comparing to Arrays and Sorting

    Can't you just use VLookup and filter on the Errors?

  8. #8
    Registered User
    Join Date
    01-02-2013
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by Kyle123 View Post
    Can't you just use VLookup and filter on the Errors?
    How might you do that Kyle?

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Comparing to Arrays and Sorting

    In your sheet of Business names, add a formula that checks the business name against the secondlist, so assuming Business Name is in A1 and your second list is in Range G1:G100
    B1:
    PHP Code: 
    =VLookup(A1,$G$1:$G$100,1,False
    Drag it down

    Then use Autofilter, anything with an Error, isn't in your second list

  10. #10
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by ebase131 View Post
    I don't know SQL unfortunately, but how might one go about using it if I'm new to it?
    Try this: http://lmgtfy.com

  11. #11
    Registered User
    Join Date
    01-02-2013
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by Kyle123 View Post
    In your sheet of Business names, add a formula that checks the business name against the secondlist, so assuming Business Name is in A1 and your second list is in Range G1:G100
    B1:
    PHP Code: 
    =VLookup(A1,$G$1:$G$100,1,False
    Drag it down

    Then use Autofilter, anything with an Error, isn't in your second list


    Would the remaining list then be able to be used/adapted to a new data table? I guess I'm worried about using Autofilter as it will take me out of the macro and force the user to do things instead of making the entire process automated.

  12. #12
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Comparing to Arrays and Sorting

    I'm intrigued, in what way would SQL solve this more easily than VLookup?

    You could copy the list to a new sheet and automate the process - that way the autofilter won't interfere with anything. You can then copy all the visible cells into a new sheet/wherever you want them.

    Having re-read your question (should have read it properly to start with ) things are a little more complex if the business names aren't exactly the same, you'd need to implement a "fuzzy" matching routine - have a look here: http://www.mrexcel.com/forum/excel-q...planation.html
    Last edited by Kyle123; 01-08-2013 at 12:23 PM.

  13. #13
    Registered User
    Join Date
    01-02-2013
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by Kyle123 View Post
    I'm intrigued, in what way would SQL solve this more easily than VLookup?

    You could copy the list to a new sheet and automate the process - that way the autofilter won't interfere with anything. You can then copy all the visible cells into a new sheet/wherever you want them.

    Having re-read your question (should have read it properly to start with ) things are a little more complex if the business names aren't exactly the same, you'd need to implement a "fuzzy" matching routine - have a look here: http://www.mrexcel.com/forum/excel-q...planation.html
    Interesting, I will give it a look. Thank you for the responses.

    You think this is a more viable option than the SQL technique?

  14. #14
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Comparing to Arrays and Sorting

    yes, a SQL implementation for this sounds like a bad idea to me, it's complex and you can't join on partial matches.

  15. #15
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by Kyle123 View Post
    I'm intrigued, in what way would SQL solve this more easily than VLookup?
    Matches:
    Please Login or Register  to view this content.
    Unmatched:
    Please Login or Register  to view this content.
    Two lines of code.


    OR, get one result set showing everything:
    Please Login or Register  to view this content.
    One line of code. And it's sorted alphabetically.

  16. #16
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Comparing to Arrays and Sorting

    It's the set-up that's complex - you'd need to execute the query from another workbook and that complicates things - so it's a lot more than 2 lines of code.

    Additionally the business names don't match, so you can't join - I missed that one too with my vlookup suggestion
    Last edited by Kyle123; 01-08-2013 at 01:58 PM.

  17. #17
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Comparing to Arrays and Sorting

    Well, I'd just import the data into Access (or copy and paste into tables) and run the code from there. I could stick with VBA and use ADO but that would be the wrong tool for the job IMHO.

    'not sure about the 'business names' thing. I couldn't see where the OP mentioned them.

  18. #18
    Registered User
    Join Date
    01-02-2013
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by PingPing View Post
    Well, I'd just import the data into Access (or copy and paste into tables) and run the code from there. I could stick with VBA and use ADO but that would be the wrong tool for the job IMHO.

    'not sure about the 'business names' thing. I couldn't see where the OP mentioned them.
    The business names might not exactly match what is currently listed in the "current customers" list, there may be a typo or a name might be truncated from the full name by a few letters.

  19. #19
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by ebase131 View Post
    The business names might not exactly match what is currently listed in the "current customers" list
    Okay, so by definition they don't match and will be returned as such in the query's result set. I'm missing something here. Isn't that what you want?

    As to partial matches, you can always alter the JOIN predicate to accomodate matching a set number of characters, eg. to match on the 1st 10 characters in each list:
    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    01-02-2013
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by PingPing View Post
    Okay, so by definition they don't match and will be returned as such in the query's result set. I'm missing something here. Isn't that what you want?

    As to partial matches, you can always alter the JOIN predicate to accomodate matching a set number of characters, eg. to match on the 1st 10 characters in each list:
    Please Login or Register  to view this content.
    I think the partial match is the way to go. That sounds like a good idea.

    The end goal is to have a list that will match up these customers despite the typos/truncation so that their sales quantities and dollars can be added to the existing lists. Any new customers or new products ordered by existing customers will require the new rows added in the updating sales spreadsheet.

  21. #21
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Comparing to Arrays and Sorting

    FYI, if you do come across any partial matches then the problem should be corrected at source, ie. go back to the original data entry and have it corrected so that it doesn't repeat itself. Btw, using integer-only CustomerIDs is a better way to track customers than using strings - you get less typos/truncations with integers.

    At some point you may want to give some thought to migrating away from spreadsheets and towards a proper database solution. As a developer, over the years, I've come across some outright abuses of Excel that should never have been allowed to happen. Spreadsheets have their place but their ease of use can create problems.
    Last edited by PingPing; 01-08-2013 at 02:35 PM.

  22. #22
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by PingPing View Post
    I could stick with VBA and use ADO but that would be the wrong tool for the job IMHO.
    Me too since you can't query the current workbook with ADO. As PingPing mentions, you shouldn't really allow people to free type existing data, use drop downs instead - makes things much easier

  23. #23
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by Kyle123 View Post
    you can't query the current workbook with ADO.
    You can, but it creates a memory leak - see http://support.microsoft.com/kb/319998.

    The way I got around this was to save a copy of the workbook to c:\temp and ADO against that - as they recommend. Although, to be fair, I wasn't repeatedly querying the workbook in the same Excel session, so I didn't really need to implement the workaround.

  24. #24
    Registered User
    Join Date
    01-02-2013
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Comparing to Arrays and Sorting

    Quote Originally Posted by Kyle123 View Post
    Me too since you can't query the current workbook with ADO. As PingPing mentions, you shouldn't really allow people to free type existing data, use drop downs instead - makes things much easier
    Well the main issue is the raw data comes from a separate program's ouput, but that output can be sent out as an Excel spreadsheet. Once I get that output, it's in a set format so I at least know what I'm going to get, but the output from the program has some customer names that are truncated or not labeled exactly as the existing customer list.

    I do think there is a way to make an un-exact match in VB that can match most of the letters in the existing customer list.

  25. #25
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Comparing to Arrays and Sorting

    The memory leak was the reason I was saying you can't do it. might be a worthwhile exercise seeing if dao has the same issue
    Last edited by Kyle123; 01-08-2013 at 02:56 PM.

+ 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