+ Reply to Thread
Results 1 to 18 of 18

Match and Countif within Array

  1. #1
    Registered User
    Join Date
    04-12-2011
    Location
    Bay Lake, FL
    MS-Off Ver
    Excel 2016 / 365
    Posts
    66

    Match and Countif within Array

    I've got a fairly large (~230k rows) data set, with ID numbers in Column A. The ID numbers are not unique, a particular ID could appear once in the dataset, or it could appear 20 times. As part of my process, I'm sorting by the ID number, then I need to find the first row number that a particular ID appears on, and the number of times that it appears in the dataset.

    Right now, I'm doing that in a For loop, and it's painfully slow.
    Please Login or Register  to view this content.
    I've dumped the data into an array, but the Match function doesn't seem to work the same way within the array as it does when I'm just looking at the Range. It's returning all zeros, probably because I don't have the right syntax to look at all rows (rather than just the RowCountVariable row) of the array. The Red text is where I'm going wrong. (And I haven't even tried the Countif line yet, because I can't get the Match, and that's necessary for the other)
    Please Login or Register  to view this content.
    Last edited by SyracuseWolvrine; 05-17-2022 at 04:50 PM. Reason: solved

  2. #2
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Match and Countif within Array

    Hello SW:
    There are 3 things you can do to make this much faster.
    (1) Minimize your search area / id. Since you know that your max rows for each ID (i.e. 20) AND that they are sorted THEN only search 20 rows .
    This is pseudo indexing of your data and should make a tremendous difference in your time (SEE CODE BELOW FOR EXAMPLE OF 20 ROW SEARCH RANGE)
    (2) Write your results to an array and after 10,000 entries to array then dump to sheet and keep doing this until done . Dumping to sheet only once every 10,000 results will make a tremendous diffence !
    (3) Turn off screen updating and autoCalculation during process ... if you use the two items above this will make only a small difference.

    SEE VERSION 2 & 3 in seperate posts below this one


    NOTE: Since you did not provide a data sample I had to make some assumptions . If you provide a data sample I might be able to refine the approach.

    Please Login or Register  to view this content.
    Last edited by nimrod1313; 05-17-2022 at 11:12 AM.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Match and Countif within Array

    Quote Originally Posted by SyracuseWolvrine View Post
    I've got a fairly large (~230k rows) data set, with ID numbers in Column A. The ID numbers are not unique, a particular ID could appear once in the dataset, or it could appear 20 times. As part of my process, I'm sorting by the ID number, then I need to find the first row number that a particular ID appears on, and the number of times that it appears in the dataset.
    Do you need to do this with array?
    If you need it to, you will need to loop.
    Use of Excel function in vba.
    Assuming E1 has ID to be searched.
    Please Login or Register  to view this content.
    Last edited by jindon; 05-17-2022 at 10:19 PM.

  4. #4
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Match and Countif within Array

    SO I RAN CODE ON 250,000 RECORDS THAT CONTAINED 16671 ID'S (total run time 1.82 seconds)

    If the code searches the entire range for each ID then the process takes 3 minutes and 33.92 Seconds ie. 3:33.92 ( i.e. your conventional method)
    BUT ... Since you never have more that 20 of any ID, then change the code to search small areas and then the code takes only 1.82 Seconds ( see pseudo index method in code below)
    IN SHORT : pseudo indexing data has made the search 11703.30% more efficient.


    This is without Arrays or even turning off display updates or autocalc .

    Complete code is ...

    Please Login or Register  to view this content.
    ASSUMPTIONS:
    - ID'S sorted prior to running macro
    - sheet name is TestData
    - Data in column A of TestData sheet

    Note: No additional column of ID's are required as they are picked out of the complete raw data column.
    Last edited by nimrod1313; 05-17-2022 at 04:26 AM.

  5. #5
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: Match and Countif within Array

    IF ID is sorted in ascending order try this code to get First and Last row of ID.
    Please Login or Register  to view this content.
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  6. #6
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Match and Countif within Array

    VERSION 2
    Processes 250,000 ID records in 1.6 SECONDS
    added lines to decrease background overhead (e.g. calculation:=Manual, screenUpdate:=off)
    added a fourth column of output to include frequency of each ID
    added some more comments to explain code

    ASSUMPTION: ID's have been sorted prior to running macro
    APPROACH : Minimize search range of each ID to speed up macro ( RESULT WAS 11703.30% more efficient ie. run time 1.6 seconds vs 3.5 minutes)

    Please Login or Register  to view this content.
    Last edited by nimrod1313; 05-17-2022 at 12:23 PM.

  7. #7
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Match and Countif within Array

    VERSION 3 (run time 1/4 second )
    Processes 250,000 ID records and saves 16668 records to sheet in: .21 of a SINGLE SECOND

    APPROACH :
    (1) Minimize search range of each ID to speed up macro ( RESULT WAS 11703.30% more efficient ie. run time 1.6 seconds vs 3.5 minutes)
    (2) Use custom dump class to Dump all results to spreadsheet in a single transfer instead of 16668 individual actions (RESULT run time decreased to .21 second)
    Custom "dump class" (i.e. ClsBulkUpdaters) collects results to array and ONLY dumps data from array to sheet when macro has completed entire collection of all results.
    NOTE: with bulk dumps to sheet screenupdating and calculation DO NOT need to be tampered with or turned off

    DEMO ONLY:
    This version uses my custom "dump class" (i.e. ClsBulkUpdaters) which is not provided in this sample ... this version is provided to demonstrate how bulk dumps can speed up macros and that turning off applications features is not always needed.
    It also demonstrates the principle of abstracting reusable code into it's own class so that it is a quick and simple object to reuse without much complication or fuss.
    IN SUMMARY USING VERSION 2 above will be still under 2 seconds and probably be timely enough for your needs.

    Please Login or Register  to view this content.
    Last edited by nimrod1313; 05-17-2022 at 12:22 PM.

  8. #8
    Registered User
    Join Date
    04-12-2011
    Location
    Bay Lake, FL
    MS-Off Ver
    Excel 2016 / 365
    Posts
    66

    Re: Match and Countif within Array

    Thank you all for your help and advice. Thank you in particular to nimrod1313, for taking the time to provide 3 different versions, and explaining the difference between them.

  9. #9
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Match and Countif within Array

    Cheers I'll be interested in hearing how it works out for you.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Match and Countif within Array

    You are welcome and thanks for the rep.

    Execution of all my codes on 250000 lines was within 0.5 seconds, especially "IfSorted" is too fast to measure, btw.
    Last edited by jindon; 05-17-2022 at 08:57 PM. Reason: Typo...

  11. #11
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Match and Countif within Array

    Quote Originally Posted by jindon View Post
    You are welcome and thanks for the rep.

    Execution of all my codes on 250000 lines was within 0.5 seconds, especially "IfSorted" is too fast to measure, btw.
    Hello jindon:
    I've tried your "ifSorted" code on my 250000 record set with 16668 different ID's.
    When I run my code i get all 16668 ID's found and their started and ending rows recorded on the sheet in under .20 of a second.
    BUT using the same 250000 data set your's is only finding the first ID location .... AM I MISSING SOMETHING

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Match and Countif within Array

    Oops, it was the Last row only.

    I fixed the code already.

  13. #13
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Match and Countif within Array

    Hello Jindon :
    I did get your code to run for all 16668 ID's in 250000 in an impressive 1.6 seconds ... WOW ... GREAT !
    HOWEVER the "FirstRow" and "LastRow" always gave the same number AND it was always off by 1.
    Eg. My data set had the first ID go from row 2 to row 11 , while your macro indicated the FirstRow and LastRow as (10).

    Here's the code I tested with ...

    Please Login or Register  to view this content.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Match and Countif within Array

    Tested 250000 with 1668 unique IDs.
    It shows like

    First Row = 224531
    Last Row = 224682
    Took : 0.0078 sec

  15. #15
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Match and Countif within Array

    Hello Jindon , Good-day
    The original poster states
    a particular ID could appear once in the dataset, or it could appear 20 times
    That would mean then number of unique ID's you test with for 250000 records is 12500 unique ID's, not 1668 . That's what I tested you code with and got an impressive time of 1.6590 seconds .
    But the original poster also wanted "the number of times that it appears in the dataset". When I run your "IfSorted" code I'm not seeing a return of the frequency of each ID.
    In fact I'm getting the same number for the first and lastrow number and it's neither . (please see screen shot below).
    I had to modify your code to run for all unique ID's ... did I do something wrong with you code (Please see attached code)

    Please Login or Register  to view this content.
    Attachment 780896

    SearchResults.jpg
    Last edited by nimrod1313; 05-18-2022 at 04:18 AM.

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Match and Countif within Array

    Doesn't make any difference.

    Tested with 250000 rows with 108140 unique IDs, with min 1 to max 6 appearances for each ID.

    My little old PC shows like

    First Row = 10129
    Last Row = 10130
    Took : 0.0000 sec
    Attached Files Attached Files

  17. #17
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Match and Countif within Array

    Hi Jindon:
    Thanks so much for taking the time with me .
    Code this quick is worth understanding .
    I will look at code later today .
    Cheers

  18. #18
    Registered User
    Join Date
    04-12-2011
    Location
    Bay Lake, FL
    MS-Off Ver
    Excel 2016 / 365
    Posts
    66

    Re: Match and Countif within Array

    Just now finding time to loop back to this post.

    When all is said and done, I was able to get the whole process down to about 40 seconds. (it was previously taking 45+ minutes, so I'm happy with 45 seconds.)

    The CountIf and finding row numbers is just part of a larger process which involves copying data from 1 file to another, multiple concatenations, trims, removing specific characters, etc. The actual process for the countif part is now down to about 2 seconds, using this code (You'll note that maximumrowstosearch is set to 15,000 ... that's not a typo, I found there was one ID that appeared in the original source document 14,237 times, so I rounded up a little to be on the safe side)

    Please Login or Register  to view this content.

+ 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. [SOLVED] Need array formulas for Countif that counts row by row and an array Mod formula
    By Slickback724 in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 4
    Last Post: 11-09-2021, 12:38 AM
  2. Replies: 5
    Last Post: 02-22-2018, 01:47 AM
  3. CountIf Index Match Array not sure ?
    By Prospidnick in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 09-13-2017, 09:39 AM
  4. Replies: 3
    Last Post: 04-02-2016, 08:16 PM
  5. [SOLVED] Array Match and Return value upon multiple array match criteria
    By VegetaOSX in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-05-2015, 05:50 AM
  6. [SOLVED] Array, VLOOKUP - or- Match/index with a Countif.
    By JasonNeedsHelp in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-15-2015, 05:38 PM
  7. Replies: 2
    Last Post: 03-16-2012, 12:03 PM

Tags for this Thread

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