+ Reply to Thread
Results 1 to 5 of 5

How to make UDF search using InStr run faster?

  1. #1
    Registered User
    Join Date
    01-07-2020
    Location
    Morton, Illinois, USA
    MS-Off Ver
    365 Business
    Posts
    6

    How to make UDF search using InStr run faster?

    I've got a very simple UDA to search for text in each cell of a 1 dimensional array. See attached. It runs pretty fast until its combined with a few other Excel functions and given a limited number of row to work on. Any idea how to make it run faster?
    Attached Files Attached Files
    Last edited by asaxsma; 01-08-2020 at 02:06 PM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: How to make UDF search using InStr run faster?

    As a first comment: you are using the full column B ... it takes time, just limit the range to what is needed: B7:B21
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,925

    Re: How to make UDF search using InStr run faster?

    A few comments:

    As a rule, any VBA operation is slower than an equivalent Excel formula only operation due to the overhead needed for VBA and Excel to talk to each other. It appears to me that your current operation is equivalent to =IFERROR(INDEX(data!A,MATCH("*"&A4&"*",data!C,0)),"not found") where data!C contains a formula to convert any single entry numbers to text =IF(ISTEXT(A2),B2,TEXT(B2,"General")). This approach uses a slow exact match linear lookup, but is still much faster than your UDF.

    As noted, you are using full column references, and your UDF has no code for testing "used range". So the UDF goes through all 1 million rows in column B -- even though almost 1 million of those rows are blank. Limiting the range passed to the UDF should dramatically help performance. If you want to be able to pass full column references, you will want some way for the procedure to detect "last used row" so that it will stop when it gets to that point.

    You have two calls to your UDF inside of each formula -- effectively duplicating that effort. I would expect some improvement from putting the udf-instrsearch() function into a helper cell, so your final formula does not need to repeat the lookup twice per formula.

    Finally, you are using OFFSET() instead of INDEX(). OFFSET() is volatile, so every copy of the formula will recalculate with each calculate event -- even when nothing has changed.

    Unless there is some other reason that I am not seeing to use a VBA UDF, I would be inclined to use the INDEX(...MATCH()) function with an exact, wildcard match. If there are other reasons you are required to use VBA for this, I would spend some time with the code to improve the lookup and how it handles full column references. Maybe even call the MATCH() function from within VBA (https://docs.microsoft.com/en-us/off...n-visual-basic ).
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    01-07-2020
    Location
    Morton, Illinois, USA
    MS-Off Ver
    365 Business
    Posts
    6

    Re: How to make UDF search using InStr run faster?

    I was not aware that match() had wild care lookup. That did the trick! Excel formula is much quicker! That certainly took care of my immediate need. Thank you!

    However, now I'm thinking that I'd like to add more smarts to it. I'd like it to continue to search rows past the first positive match for a match of criteria in other columns. I'm guessing I'll need VBA for that?

    Maybe instead of creating a UDF, maybe I should tie it to a button press to so that its not trying to recalculate all the time. Thoughts?

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,925

    Re: How to make UDF search using InStr run faster?

    You might need VBA for what you are asking -- it isn't clear to me exactly what you want to do next. However, what you describe sounds more like a filter, so I would start by exploring the autofilter (and maybe advanced filter) tools to see if I could achieve my goal using these built in tools before turning to VBA.

    Autofilter: https://www.wikihow.com/Use-AutoFilter-in-MS-Excel
    Advanced filter: https://www.contextures.com/xladvfilter01.html

    At this point, the problem also starts to feel like something that power pivot/power query/get and transform might also handle better than VBA. I have no experience with these tools, so I cannot help with specifics, but you might find it worthwhile to become familiar with what these tools can do (if you frequently need to query/filter your data).

+ 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. Make a Faster Search.
    By candyfreak in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-23-2012, 03:57 AM
  2. [SOLVED] Workbook Name InStr Search
    By lloydgodin in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-11-2012, 09:38 AM
  3. is there an equal fxn for 'InStr' in excel. Not Find or Search
    By Arvi Laanemets in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-06-2005, 12:05 PM
  4. [SOLVED] is there an equal fxn for 'InStr' in excel. Not Find or Search
    By Clausius in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  5. is there an equal fxn for 'InStr' in excel. Not Find or Search
    By Clausius in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 01:05 AM
  6. [SOLVED] is there an equal fxn for 'InStr' in excel. Not Find or Search
    By Clausius in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM
  7. [SOLVED] is there an equal fxn for 'InStr' in excel. Not Find or Search
    By Clausius in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM
  8. [SOLVED] is there an equal fxn for 'InStr' in excel. Not Find or Search
    By Clausius in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-30-2005, 04:05 PM

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