+ Reply to Thread
Results 1 to 34 of 34

VBA code to replace INDEX/MATCH in large dataset

  1. #1
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Question VBA code to replace INDEX/MATCH in large dataset

    Hi there,

    I have a large dataset using unique # for every customer, over 450k of them. Each of these customers also has a customer ID number.

    I have a set of data where the # has been extracted but it should have been the customer ID number.

    I have this code to match the customer ID to the # but due to the number or records, it slows excel a lot.

    =IF(ISNA(INDEX(J:J,MATCH(C4,I:I,0))),"",INDEX(J:J,MATCH(C4,I:I,0)))

    Is there a VBA solution to do exactly what that formula does, which would used less memory process please?

    Thanks,

    Ian

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,781

    Re: VBA code to replace INDEX/MATCH in large dataset

    Please upload sample workbook with enough data (approx 20~50 rows) representing your actual data (of course sanitize it so you don't share any confidential info).

    This sort of operation can be done via PowerQuery, MSQuery or VBA (using dictionary & Array etc).

  3. #3
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    Will do - it's VBA I'm after please.

  4. #4
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    And here is a sample sheet - have left columns as I need them for adapting any solutions in VBA - the formula in column D uses too much memory over 450k rows of data.
    Attached Files Attached Files

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA code to replace INDEX/MATCH in large dataset

    If you can sort your lookup table by the Hash column, a binary search should be much faster
    =IF(ISNA(VLOOKUP(C2,$I$2:$I$30,1)),"",IF(VLOOKUP(C2,$I$2:$I$30,1)=C2,VLOOKUP(C2,$I$2:$J$30,2),""))
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,781

    Re: VBA code to replace INDEX/MATCH in large dataset

    Based on VLOOKUP code found in link below.
    http://analystcave.com/excel-vlookup...l-performance/

    It also has very good article on formula, SQL lookup as well.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    Thanks - due to the number of rows in my actual dataset, have had to amend to this;

    =IF(ISNA(VLOOKUP(C2,$I:$I,1)),"",IF(VLOOKUP(C2,$I:$I,1)=C2,VLOOKUP(C2,$I:$J,2),""))

    Which is coming up with nothing.

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA code to replace INDEX/MATCH in large dataset

    Did you sort columns I and J by column I first?

  9. #9
    Forum Expert
    Join Date
    05-05-2015
    Location
    Waterlooville,England
    MS-Off Ver
    Office 2010
    Posts
    26,610

    Re: VBA code to replace INDEX/MATCH in large dataset

    Try

    =IF(ISNA(VLOOKUP(C2,$I:$I,1)),"",IF(VLOOKUP(C2,$I:$I,1,0)=C2,VLOOKUP(C2,$I:$J,2,0),""))

    if unsorted

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA code to replace INDEX/MATCH in large dataset

    Quote Originally Posted by JohnTopley View Post
    Try

    =IF(ISNA(VLOOKUP(C2,$I:$I,1)),"",IF(VLOOKUP(C2,$I:$I,1,0)=C2,VLOOKUP(C2,$I:$J,2,0),""))

    if unsorted
    Would that not put us back where we began? The crux of this solution is using the binary search which is hugely quicker than an exact match approach.

  11. #11
    Forum Expert
    Join Date
    05-05-2015
    Location
    Waterlooville,England
    MS-Off Ver
    Office 2010
    Posts
    26,610

    Re: VBA code to replace INDEX/MATCH in large dataset

    You are correct BUT (rightly or wrongly) there appears to be a consensus that INDEX/MATCH is better which is where the OP started!

    CK76's VBA solution is likely to prove the winner.

  12. #12
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,781

    Re: VBA code to replace INDEX/MATCH in large dataset

    Yep, on unsorted list, Index,Match or Vlookup it doesn't really matter. Performance will lag considerably on large data set (100k + Lookup).

    To leap benefit of double approximate Vlookup's speed, you'd need lookup table to be sorted on key column.

    Personally I prefer using PowerQuery for simplicity of managing setup (though slightly slower than VBA/MS Query/ADODB).

  13. #13
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    Hi all,
    Love a lively topic
    The VBA did indeed crack it as I'd suspected - was getting considerable lag on all of the other solutions, and the volume of hashtags, with differences in numbers or text being the first character, confused the sorting a bit.
    Thank you CK76 - will shout if I have any problems on the full dataset but just tried a sample and it worked a treat.
    Ian

  14. #14
    Forum Guru Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: VBA code to replace INDEX/MATCH in large dataset

    One of the problems that I see is that you're doing the lookup twice.

    Once to test for an error and the other to do the actual lookup.

    If you're using Excel 2007 or later use the IFERROR function.

    =IFERROR(VLOOKUP(...),"")

    That way you only do the lookup once.
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  15. #15
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    Thanks for that - I shall give that a go also

  16. #16
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    CK76 sir - I've run into a little issue with the VBA code you suggested, in that it's started throwing up an error message if the match is longer than about 10k rows at a time, which when working with 450k records is proving tricky.

    I adapted it a little and it's attached - but the error was coming up before I changed the rows referenced in range section at the top of the code.

    Sorry to resurrect an old thread but are you able to suggest a tweak please?

    Ian

    error1.jpg

    error2.jpg
    Attached Files Attached Files
    Last edited by iantix; 02-10-2017 at 01:06 PM. Reason: adding excel sheet

  17. #17
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    Rather annoyingly - the issue is not happening on my slimmed down sheet on the previous post, as I had to get the file under 1mb to attach it.

    In the original, columns D and E have 450k emails with a numerical value corresponding to it in column E.

    Can wetransfer the larger file if needs be - all emails were anonymised/random generated for this version.

  18. #18
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,781

    Re: VBA code to replace INDEX/MATCH in large dataset

    What the error code tells me is that you have duplicate key string you are trying to add to Scripting.Dictionary.

    Use Debug.Print or step through the code and see which key string is returning error.
    Last edited by CK76; 02-10-2017 at 01:21 PM. Reason: Grammer

  19. #19
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    Can you see my images attached above which show the debug screen?

  20. #20
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    The only row that comes up on debug is this one;

    Function BuildLookupCollection(categories As Range, values As Range)
    Dim vlookupCol As Object, i As Long
    Set vlookupCol = CreateObject("Scripting.Dictionary")
    For i = 1 To categories.Rows.Count
    If categories(i) <> "" Then
    Call vlookupCol.Add(CStr(categories(i)), values(i))
    End If
    Next i

    Set BuildLookupCollection = vlookupCol
    End Function

  21. #21
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,781

    Re: VBA code to replace INDEX/MATCH in large dataset

    What I mean is that...

    Add following line in the code just before the line that produces error and see what comes up in immediate window.
    Please Login or Register  to view this content.
    Code uses CStr(Categories(i)) as key string. It's taken from your column D. There is duplicate value there.

  22. #22
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    Thanks - am back in office Monday so will do this then and report back. Many thanks.

  23. #23
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    Okay - I pasted the extra line, ran the macro, same error and here's a screen of what it shows on debug - did I past the extra line of code in the place you wanted me to?

    error3.jpg

  24. #24
    Forum Contributor
    Join Date
    08-29-2012
    Location
    Slough
    MS-Off Ver
    Excel 2007
    Posts
    469

    Re: VBA code to replace INDEX/MATCH in large dataset

    iantix, you need to press Ctrl + G to get the immediate window pop up. Then just copy and paste what it says onto here, that will help ascertain what the problem is.

  25. #25
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    Thanks - well what it shows are actual emails and their relevant ids, from row 171219 to 171417 - 199 pairs of email and id/rows (of a total of 488673 rows of emails and ids to match, including header row).

    I'm unsure why it's falling over with this range - nothing looks out of ordinary on the data, which I can't share on the 'live' version. My test one above hasn't that much data as I have no easy way of producing 200k random false emails for testing.
    Last edited by iantix; 02-13-2017 at 07:33 AM.

  26. #26
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    I have started a new workbook, ro rule out issues on the older one - copied and pasted all values, then copied just the module code for the index/match. Saved separately as just one sheet in the book, 15mb filsesize.

    Restarted machine.

    Same exact error, same exact place - the word 'immediate' appears at the top of the box containing the 199 rows of data.

  27. #27
    Forum Contributor
    Join Date
    08-29-2012
    Location
    Slough
    MS-Off Ver
    Excel 2007
    Posts
    469

    Re: VBA code to replace INDEX/MATCH in large dataset

    I am not 100% sure but are you sure that there isn't a duplicate ID in there? Just below the 171,417th record?

  28. #28
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    I had that thought myself and am checking now - standby

  29. #29
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,781

    Re: VBA code to replace INDEX/MATCH in large dataset

    The last string that appears in immediate window. That should be duplicate.

    Remove that and test.

  30. #30
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    Thanks - we're cleaning data this end as it does indeed appear that there are doubles.

    Is it possible, in theory, to have the code skip or flag doubles - or even return both values associated with that email address (maybe one in one column, one in another?).

    Just curious how much one could do via the VBA.

  31. #31
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,781

    Re: VBA code to replace INDEX/MATCH in large dataset

    Is it possible, in theory, to have the code skip or flag doubles - or even return both values associated with that email address (maybe one in one column, one in another?).
    Yes it is possible. However, you'll need to alter code a bit.

    Skip is easy, just change BuildLookupCollection function as below.
    Please Login or Register  to view this content.
    You can add flag code before the inner Else statement.

    Returning both values, is going to be an issue. You will need to use methods other than using Dictionary object, or significantly alter code logic.

  32. #32
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    Thanks - even this is a distinct improvement for me at this stage - I can filter what we know to have unique values, while a dev cleans the database by merging records.

    It's actually flagged a serious issue here which is being resolved so worth the back & forth.

    I shall report back on the above and eventual clean of original data. Thank YOU for your help CK76.

  33. #33
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    231

    Re: VBA code to replace INDEX/MATCH in large dataset

    All I got do now is work out the flag code bit - fairly new to VBA, but asking manager for a course on it to get more familiar.

  34. #34
    Registered User
    Join Date
    11-25-2012
    Location
    hk
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: VBA code to replace INDEX/MATCH in large dataset

    Quote Originally Posted by CK76 View Post
    Based on VLOOKUP code found in link below.
    http://analystcave.com/excel-vlookup...l-performance/

    It also has very good article on formula, SQL lookup as well.

    Please Login or Register  to view this content.
    Sorry, how can we adjust to work as 2-dimension index/match
    Capture.JPG
    i know how to do it with formula (as attached logic) but don't know how i can shortern the process time.... i have >40,000 rows in the file and need to pops up results for >20 columns..
    Last edited by teireii; 07-26-2017 at 03:15 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Index match with large function
    By morobo in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-19-2014, 02:27 PM
  2. Replies: 3
    Last Post: 01-23-2014, 10:13 AM
  3. VBA Code to Replace Cell Value when Address found by Index/Match
    By Rubert in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-23-2013, 10:27 AM
  4. INDEX, MATCH and LARGE
    By ridebikes in forum Excel General
    Replies: 1
    Last Post: 10-24-2011, 04:03 PM
  5. Using INDEX/MATCH in combination with LARGE?
    By scoobz in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-06-2008, 02:38 PM
  6. Index, Match, and Large
    By edwardpestian in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-03-2006, 06:10 PM
  7. Large Index Match Lookup
    By Qaspec in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 04:05 PM
  8. [SOLVED] Large Index Match Lookup
    By Biff in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-06-2005, 04:05 AM

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