+ Reply to Thread
Results 1 to 20 of 20

Loop Alternatives/performance increases

  1. #1
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Loop Alternatives/performance increases

    Hi,

    I use the code below on a couple of combo boxes to help filter some data, it's relatively quick but there is a noticeable pause. is there a method of reducing this pause, i'm looking into variant arrays but its taking time to sink it and "click" but wondering if there's anything else?

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Loop Alternatives/performance increases

    You could try this... (you need to add back the Application settings at the start and end):
    Please Login or Register  to view this content.
    I don't understand the Champion & Challenger block.
    Please Login or Register  to view this content.
    What's the purpose of the 2 additional IF... statements in the CASE block?

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop Alternatives/performance increases

    Possibly use Excel's Autofilter capability to hide/unhide rows instead of looping through rows doing it manually. That should be instantaneous.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Loop Alternatives/performance increases

    Hi,

    thank you for your repsonse, i've tried the code and it doesn't seem to allow the rows to be unhidden if all is selected? the purpose of the champion and challenger block is to allow two "catergories" to be visible while the others are hidden.

  5. #5
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Loop Alternatives/performance increases

    Quote Originally Posted by JBeaucaire View Post
    Possibly use Excel's Autofilter capability to hide/unhide rows instead of looping through rows doing it manually. That should be instantaneous.
    I'm trying to replicate that functionaility actually, and due to how this sheet has been constructed and other sheets dependencies on this sheet its not ideal to use autofilters.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop Alternatives/performance increases

    The AutoFilter allows multiple items to be shown while all others hidden. Correcting design errors to allow the Autofilter to function correctly would be the ideal path.

  7. #7
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Loop Alternatives/performance increases

    Quote Originally Posted by JBeaucaire View Post
    The AutoFilter allows multiple items to be shown while all others hidden. Correcting design errors to allow the Autofilter to function correctly would be the ideal path.
    While I agree with you entirely, its just not feasible here. Re-constructing this sheet would then require many many hours re-constructing other sheets and formula's - so a work around/alternative it must be.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop Alternatives/performance increases

    Then your existing code is pretty much how to do it. You're individually setting the hidden property of 90 rows each and every time you change the combo box. That means 90 updates to the sheet, that's the pause, and that will take as long as it takes. You've turned off everything you can to speed it up, from what I can see.

  9. #9
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Loop Alternatives/performance increases

    Thank you.

  10. #10
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop Alternatives/performance increases

    Quote Originally Posted by Kramxel View Post
    While I agree with you entirely, its just not feasible here. Re-constructing this sheet would then require many many hours re-constructing other sheets and formula's - so a work around/alternative it must be.
    Hi,

    Would you like to share your workbook with is so that we can check whether the autofilter is unfeasible.
    JB is right, it's a particular hobby horse of mine. I've lost track of the number of times I've advised people that loops should only ever be the last resort for this sort of stuff for the very reasons you are encountering. An Autofilter is the fastest way of doing this. This really should be a FAQ!

    If your existing columns don't easily lend themselves to autofiltering exactly what you want easily then a technique I often use is to add a helper column which works out from the other columns with various IF AND OR tests whether the particular row should be filtered. Then this single helper column can be used as the autofilter. There's no need to reconstruct the worksheet at all.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  11. #11
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Loop Alternatives/performance increases

    Hi..

    If you can't use autofilter.. and you're stuck with a loop.. maybe at least try building a range using union and then hiding the range in one hit (at the end once the range is built).. rather than doing it every time a cell meets the condition.

  12. #12
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Loop Alternatives/performance increases

    Quote Originally Posted by apo View Post
    Hi..

    If you can't use autofilter.. and you're stuck with a loop.. maybe at least try building a range using union and then hiding the range in one hit (at the end once the range is built).. rather than doing it every time a cell meets the condition.
    I *think* that's what I've been reading about i'm just not understanding it quite yet - i'm hoping for that eureka moment when everything clicks and I realise A) how easy it actually is and B) how much of an ID10T I actually am.

  13. #13
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Loop Alternatives/performance increases

    Duplicate please ignore

  14. #14
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop Alternatives/performance increases

    If you're stuck on using a loop then a better way of processing it would be to read the range into a VBA array and then process the array with a loop and use the loop counter to refer to the array and then build a second array of those row numbers that should be hidden.

    The reason 'normal' loops take a long time is that they have to keep jumping backwards and forwards between the VBE and the Excel sheet to read and write. Each time the loop does this there is a time overhead. By reading all your data into a VBA array you process the arrays entirely in the VBE without all the jumping backwards and forwards, and only when you've finished the processing do you write the results or actions back to Excel.

    But as I said in #10 why not upload the workbook. I'll be most surprised if you can't use an autofilter to do what you want. And it'll be almost instantaneous and only need two or three lines of code as opposed to the many Selcect & Loop lines you currently have.

  15. #15
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Loop Alternatives/performance increases

    Quote Originally Posted by apo View Post
    Hi..

    If you can't use autofilter.. and you're stuck with a loop.. maybe at least try building a range using union and then hiding the range in one hit (at the end once the range is built).. rather than doing it every time a cell meets the condition.
    would you mind giving me a run down of how to do that given my example above: I like this as A) solves my immediate problem B) gives me something else to learn, but after reading up on it i'm still a little confused.

  16. #16
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Loop Alternatives/performance increases

    Untested, but something like this:

    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  17. #17
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Loop Alternatives/performance increases

    Nice combo Rory..

    @ Kramxel... Rory has combined Richards suggestion of doing it mostly within memory (read into array) and building a Range using the union method..

    You should step thru (F8) his code , monitoring the Locals Window in the VB Editor to see what is going on..

  18. #18
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Loop Alternatives/performance increases

    i [have] been refreshing thread all afternoon and only just noticed it has spilled over to a second page

    Thank you Rory and Apo just what I needed things are slowly begining to sink in - can see this being quite useful indeed, still going to take a while before it really takes hold but i'm sure i'll get there soon1
    Last edited by JBeaucaire; 10-09-2014 at 12:07 PM. Reason: Edited for content. (hat-tip)

  19. #19
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Loop Alternatives/performance increases

    having played around with this works almost flawlessly

    "The almost" is that no matter what the value in C26, if B26 is set to All, every row is unhidden when it should be all rows that have an entry in them other than what's displayed in C26 (hope that makes sense)

  20. #20
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Loop Alternatives/performance increases

    I'm done for the day, so I'll leave that as a learning exercise for you. Or someone else can fix it of course.

    If you're still stuck tomorrow, let us know.

+ 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] Numerical values increases as name increases
    By Jbelle7435 in forum Excel General
    Replies: 3
    Last Post: 09-16-2014, 01:41 PM
  2. VBA For loop, optimize speed and performance
    By britzer in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-04-2014, 08:56 AM
  3. Loop alternatives or improvement for speed
    By TommyN in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-18-2013, 06:00 PM
  4. [SOLVED] Alternatives to using a for loop in cunjunction with vlookup
    By Bobcat1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-16-2012, 09:17 PM
  5. What are the alternatives ???
    By christopherp in forum Excel General
    Replies: 4
    Last Post: 03-19-2006, 10:39 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