+ Reply to Thread
Results 1 to 7 of 7

Improve UDF speed

  1. #1
    Registered User
    Join Date
    09-07-2009
    Location
    Lytham, England
    MS-Off Ver
    Excel 2003+07
    Posts
    75

    Improve UDF speed

    Hi,

    I have made the custom function below but it needs to run against 250,000 rows and currently runs very slowly.

    I've tried to enhance where possible (based on a bit of research) but the speed is still very slow.

    IS there anyone adept and making good coding efficiencies who could assist or guide me please?

    Many Thanks
    Paul
    Attached Files Attached Files

  2. #2
    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: Improve UDF speed

    You should avoid volatile UDFs. Pass all the ranges you need to look at as direct arguments. Also, there is no point in trying to set calculation in a UDF.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,170

    Re: Improve UDF speed

    What are you wanting to achieve with this function...please explain it's purpose...
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

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

    Re: Improve UDF speed

    A few comments and observations:

    1) loops are slow, and your UDF has two loops. One good place for performance improvements is to understand what your loops are doing and see if there is a less expensive way to accomplish the same thing.
    1a) The first loop looks like a basic, brute force, linear lookup, which will be very slow. Two parts to improving this. In my experience, Excel's lookup functions are faster than a VBA loop like this. When you can use the binary search/approximate match option available through these lookup functions, it is even faster. Many of Excel's functions are available to VBA through the WorksheetFunction object: https://msdn.microsoft.com/en-us/vba...n-visual-basic The help file includes an example of using the MATCH() function with a linear search option (3rd argument=0). You can get a binary search by using 1 as the 3rd argument. This alone should dramatically improve the performance of your UDF.
    1b) The second loop looks like a basic conditional sum (SUMIF() or SUMIFS()). Again, you can use these two Excel functions, and I would expect to see some performance improvement.

    2) You say that it needs to run against 250000 rows, but it is not clear exactly what that means.
    2a) Does that mean that there will be 250000 rows in HHData * 9 function calls/row = 2.25E6 function calls? If so, you need to recognize that there is a significant overhead that is an inherent part of each UDF function call. This means that any VBA UDF, no matter how simple or optimized, is relatively slow. You can test with a very simple UDF like
    Please Login or Register  to view this content.
    Then call from the spreadsheet with =parrot(rand()). Make as many copies as you need to test this and see how long it takes to calculate (on my machine, 100000 copies of this function call took ~45 s to calculate, compared to instantaneous for 100000 copies of =RAND()). If your spreadsheet really needs that many calls to the function, then there will likely be significant performance gains by abandoning the VBA UDF and figure out how to do this with regular worksheet functions -- either nested or spread out over helper cells. As noted above, the right combination of MATCH() and SUMIFS() should replicate the UDF calculation without needing the UDF.
    2b) Or does this mean the the lookup tables in RAG Times will be 250000 rows long each? Switching to a binary lookup with the built in MATCH() function should dramatically improve this, as a binary lookup does not require significantly more time to search a large list as a small list. If this is the scenario, then simply implementing 1a may be enough performance improvement for the large lookup tables.

    Some ideas for you to consider.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Improve UDF speed

    As some have mentioned, the UDF you have provided may be possible to do with a worksheet function instead.

    I have worked out a named range and a much simpler formula using built in functions that gets the same results.

    I am testing it further to make sure it works on all columns.

    EDIT:

    Made a copy of your HHdata sheet called "HHData New". Left your old sheet as is with your functions. New sheet I used a formula that uses a named range. No macros required, same result. Should be much faster. I only applied my version to columns BO, BP, BQ. Same method can be used for other columns (creating a named range for the weekend and changing the named range in the function when moved over to columns BS, BT, BU).

    Formula in BO2:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Named range (DNOwd):

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    I did set your 2 tables on RAG times as actual tables to use structured references (tblWday and tblWEnd).

    If you have questions let me know. You likely should take a copy of your workbook, strip out the UDF code and the UDF's entered on sheet, if there is no other code save as xlsx and try my formula and compare to the copy with your UDF. Should be a pretty drastic difference in speed. Not saying it will be instant, 250k rows is alot, but should be a noticeable improvement.
    Attached Files Attached Files
    Last edited by Zer0Cool; 01-12-2018 at 03:33 PM.
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  6. #6
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Improve UDF speed

    Updated copy with an even simpler formula:

    BO2:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    I have not re uploaded, just replace the formula in BO2 with this and fill over and down. If it gives you trouble I can update the sample I sent with revised formula

  7. #7
    Registered User
    Join Date
    09-07-2009
    Location
    Lytham, England
    MS-Off Ver
    Excel 2003+07
    Posts
    75

    Re: Improve UDF speed

    Thanks guys, this works wonders. Although I have left the company I was working for doing this I will pass on.

+ 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 improve macro speed
    By Remphan in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 12-29-2015, 10:43 AM
  2. Improve Calculations Speed
    By samcdavies in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-21-2015, 09:14 PM
  3. [SOLVED] Need assistance to improve speed in looking up value
    By a_driga in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-29-2015, 08:37 PM
  4. [SOLVED] Improve speed of Sort!
    By stockgoblin42 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-29-2013, 03:12 PM
  5. [SOLVED] Need to improve speed of concatenation macro
    By engineerlady in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-01-2013, 11:19 AM
  6. Urgent Help - Improve macro speed
    By Mysore in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2012, 11:38 PM
  7. How to improve web query speed
    By hegisin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-18-2007, 12:40 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