+ Reply to Thread
Results 1 to 10 of 10

Is it possible to speed up this UDF?

  1. #1
    Registered User
    Join Date
    01-29-2020
    Location
    UK
    MS-Off Ver
    2010
    Posts
    7

    Is it possible to speed up this UDF?

    Hello! I'm using the UDF below to count specific colours in a range and with visible rows only.

    However, I'm using this on a worksheet containing 365 columns with filters and 110 rows (approx 40,000 cells), so it takes around 5-6 seconds to recalculate each time a filter is used on a column.

    I think the issue is that the function processes each cell in the specified range but I'm not sure how to do it faster.

    There's a section titled 'Faster VBA User-Defined Functions' on the Microsoft website (sorry - can't link it here because this is only my second post!) which talks about assigning a range to a variant that contains an array, and looping on that, but I'm not sure how to accomplish it, or if it's even possible with the code I'm using.

    Any help would be greatly appreciated!

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Is it possible to speed up this UDF?

    Unfortunately using an array won't work for counting colors, when you populate an array from a range all you get is the values from the range.

    What might speed things up is to only loop through visible cells - as it is the hidden cells are skipped when counting but it's still looping through the entire range.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    01-29-2020
    Location
    UK
    MS-Off Ver
    2010
    Posts
    7

    Re: Is it possible to speed up this UDF?

    Hi - I've tried the extra line of code to loop through visible cells only, but it doesn't make any difference to the speed of the calculation.

    Many thanks for the reply and suggestion, though!

  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: Is it possible to speed up this UDF?

    SpecialCells doesn't work in UDFs.
    Rory

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,431

    Re: Is it possible to speed up this UDF?

    You could give this a try.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

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

    Re: Is it possible to speed up this UDF?

    Why not upload a sample file depicting your result and I'm certain someone will provide a more efficient code...
    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!!!

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Is it possible to speed up this UDF?

    Not sure by how much, but this function should be faster as it only iterates cells in the specified range with the specified color to see if they are hidden or not and then counts only the non-hidden ones...
    Please Login or Register  to view this content.
    Last edited by Rick Rothstein; 09-15-2020 at 10:32 AM.

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

    Re: Is it possible to speed up this UDF?

    I will just throw this thought out there -- is color the only possible way to perform the count? What determines color? I would tend to go back a step to what determines color (value, category, text, or whatever) and count on that data.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  9. #9
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Is it possible to speed up this UDF?

    Taking the idea of skipping hidden rows from Andy Pope's code (Message #5) and combining it with my iterate colored cells only code (from Message #7) should produce the fastest possible color counter. Here is the UDF that does that...
    Please Login or Register  to view this content.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Is it possible to speed up this UDF?

    Quote Originally Posted by rorya View Post
    SpecialCells doesn't work in UDFs.
    That rings a bell.

+ 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. How to avoid cell SELECTION but still format cells to speed up the macro running speed
    By BeefyBerts in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-05-2018, 08:18 AM
  2. Everage Speed km/time (european speed)
    By GerryZucca in forum Excel General
    Replies: 3
    Last Post: 02-23-2015, 03:02 PM
  3. Speed up VBA Sub
    By shawnweber in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-19-2014, 07:30 AM
  4. VBA speed up
    By bensonsearch in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-18-2011, 02:46 AM
  5. [SOLVED] How to speed up VBA?
    By Cactus in forum Excel General
    Replies: 3
    Last Post: 06-14-2005, 11:05 AM
  6. Speed me up please with VBA
    By cdjsb in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-12-2005, 11:34 AM

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