+ Reply to Thread
Results 1 to 15 of 15

Searching Arrays - Application.Match or loops?

  1. #1
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Smile Searching Arrays - Application.Match or loops?

    I often try to find a specific value inside an Array.
    I have always used regular loops for this, but I recently found out that I can also use Application.Match which to me seems a lot easier to use.
    When I suggested this in one of my earlier posts on this forum, I was told Application.Match is probably slower than loops.
    I was hoping that really Application.Match would be faster, but I can't seem to find a fitting answer which I should use...

    So currently I am at a crossroads: should I start rebuilding my macros with Application.Match or should I just keep my loops?

    Just to be clear: I am NOT searching in Worksheets here - I am searching in ARRAYS, and I want to search them as fast as possible.


    Many thanks in advance!
    Last edited by Rezzy777; 04-28-2021 at 04:56 AM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,990

    Re: Searching Arrays - Application.Match or loops?

    Why don't you run some timing tests in your code?
    Everyone who confuses correlation and causation ends up dead.

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

    Re: Searching Arrays - Application.Match or loops?

    How to check an Array without loops

  4. #4
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Searching Arrays - Application.Match or loops?

    Quote Originally Posted by rorya View Post
    Why don't you run some timing tests in your code?
    I was hoping one of those two ways was already publicly known to be the fastest ;-)

  5. #5
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Searching Arrays - Application.Match or loops?

    Quote Originally Posted by jindon View Post
    Ah, know I see: I thought you were referring to the Worksheet function "Match" (my bad) ;-)

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,990

    Re: Searching Arrays - Application.Match or loops?

    If you need to search your arrays that often, perhaps you should be using some other storage mechanism, like a Dictionary or Collection.

  7. #7
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Searching Arrays - Application.Match or loops?

    Quote Originally Posted by rorya View Post
    If you need to search your arrays that often, perhaps you should be using some other storage mechanism, like a Dictionary or Collection.
    Why is that? Is there any advantage of a Dictionary or Collection over an Array when it comes to searching?

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,990

    Re: Searching Arrays - Application.Match or loops?

    You can refer to an item directly using its key instead of searching.

  9. #9
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Searching Arrays - Application.Match or loops?

    Ah, true! But then I'm limited to only one dimension, right? ;-)

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,990

    Re: Searching Arrays - Application.Match or loops?

    Yes. You haven't given any context, so I'm just throwing out ideas.

  11. #11
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Searching Arrays - Application.Match or loops?

    You are right ;-) I'll remember that when I only need 1 dimension.

    So for two dimensions I need an Array and a loop when I need to search fast (instead of Application.Match), right?

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,990

    Re: Searching Arrays - Application.Match or loops?

    Possibly. There is no "one size fits all" answer as to what is the best approach. In my opinion, if you're that bothered about the fractions of a second difference in performance, you're probably using the wrong tool anyway.

  13. #13
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: Searching Arrays - Application.Match or loops?

    Give the attached approach a try to see if meets your needs.
    it was developed to demonstrate that data accumalation after 10 years could still be sorted at acceptable speeds.
    the array is seeded by the initial 20 lines, place a number (10,000 rows takes approx 2 seconds to sort) then 'FILL ARRAY'
    the sort is on three criteria -customer code and between two dates - refer to the seed list for the range available.
    then press 'SORT' this will produce the other two sheets of sorted data and record the time of the process.
    The sort is done by filling the array with "" for those rows not meeting the criteria, then removing those rows containing "", thus leaving the data required.
    I have briefly annotated the code to indicate process.
    torachan.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Searching Arrays - Application.Match or loops?

    Quote Originally Posted by rorya View Post
    Possibly. There is no "one size fits all" answer as to what is the best approach. In my opinion, if you're that bothered about the fractions of a second difference in performance, you're probably using the wrong tool anyway.
    Didn't know the difference would be that small (fractions of a second). In that case I could go ahead and use Application.Match after all ;-)

  15. #15
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Searching Arrays - Application.Match or loops?

    Quote Originally Posted by torachan View Post
    Give the attached approach a try to see if meets your needs.
    it was developed to demonstrate that data accumalation after 10 years could still be sorted at acceptable speeds.
    the array is seeded by the initial 20 lines, place a number (10,000 rows takes approx 2 seconds to sort) then 'FILL ARRAY'
    the sort is on three criteria -customer code and between two dates - refer to the seed list for the range available.
    then press 'SORT' this will produce the other two sheets of sorted data and record the time of the process.
    The sort is done by filling the array with "" for those rows not meeting the criteria, then removing those rows containing "", thus leaving the data required.
    I have briefly annotated the code to indicate process.
    torachan.
    Wow thanks, I'll give it a try!

+ 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] Querying Large Series of Arrays And Displaying Unique Values in Arrays [Hard Index Match?]
    By larryg003 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-28-2020, 04:52 PM
  2. Application match error when used with arrays
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-10-2020, 07:34 AM
  3. Looping through similar declerations but cannot use Arrays.
    By ARowbot in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-07-2015, 10:25 AM
  4. Looping variable or arrays for boolean?
    By calvinle in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-01-2015, 12:53 PM
  5. [SOLVED] Application.Match vs very large data arrays - A.M looses
    By Rhudi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-25-2014, 04:35 PM
  6. [SOLVED] Trouble Looping Multiple Column Arrays
    By D.L. in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-23-2012, 03:32 AM
  7. Looping arrays in VBA
    By berabus in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-29-2010, 09:33 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