+ Reply to Thread
Results 1 to 15 of 15

For Each Loop (Make it Faster)

  1. #1
    Registered User
    Join Date
    05-02-2006
    Posts
    95

    For Each Loop (Make it Faster)

    Hello Experts,
    I've searched through this Forum's threads about speeding up the For Each....Next loop. There were mentions about variants, LBound, UBound, GoTo, Special Cells, Arrays, storing it in temp memory, etc but I'm unsure how to apply them into my code.

    I have 3,000+ rows. Rather than cycling through each cell and performing a command (hide row), is there a faster way to hide entire row if all cells within column A's range with the value of "Hide"? This is code currently takes 6 minutes to cycle through 3,000+ cells, one at a time.

    Please Login or Register  to view this content.
    Thanks in advance,
    Ricky

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: For Each Loop (Make it Faster)

    Hi,

    I would approach this by filtering the data, and then hiding the filtered rows:

    Please Login or Register  to view this content.
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: For Each Loop (Make it Faster)

    One thing that strikes me is that it will still be checking the cells on rows youve hidden which could (depending on the number of columns in each row) have a massive impact. eg
    if your range is A1:CV20 then you're checking 100 columns per row.
    If you find "Hide" in A1 then you're still checking B1: CV1 (99 cells) even though there is no need to do so.

    Ive had a quick test and the following doesnt work but something like this to move it after hiding the row would increase the speed.
    Please Login or Register  to view this content.
    If someone has helped you then please add to their Reputation

  4. #4
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: For Each Loop (Make it Faster)

    Quote Originally Posted by sweep View Post
    Hi,

    I would approach this by filtering the data, and then hiding the filtered rows:

    Please Login or Register  to view this content.
    if you've got 100 columns in the range though then arent you going to need to loop through the columns filtering in turn?

  5. #5
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: For Each Loop (Make it Faster)

    if you've got 100 columns in the range though then arent you going to need to loop through the columns filtering in turn?
    I don't think so, because the op is only looking to hide rows where column A="hide"

    to hide entire row if all cells within column A's range with the value of "Hide"?

  6. #6
    Registered User
    Join Date
    08-04-2009
    Location
    Golborne England
    MS-Off Ver
    2013
    Posts
    85

    Re: For Each Loop (Make it Faster)

    Depending on the version of excel you are using the simplest thing you can do to speed things up is to turn off auto calculation while running this macro. You can do this manually or via code. Currently each time you hide a row the sheet will recalculate. This takes a long time.


    Please Login or Register  to view this content.
    JimBobBowie

  7. #7
    Registered User
    Join Date
    05-02-2006
    Posts
    95

    Re: For Each Loop (Make it Faster)

    Quote Originally Posted by sweep View Post
    I don't think so, because the op is only looking to hide rows where column A="hide"
    Thank-you sweep,
    Yes, the "Hide" criteria is down column A for now. I've defined use the dim rng as range in case users inserts new column(s) in the future. However, the rest of my code requires a Data Query of another application. If filtering is used, the refresh doesn't work for the hidden rows. I'm trying to stay away from it for this process.

    What's an alternative to Select all cells within range with "Hide" then hide entire row all at once?

    I appreciate your help and everyone's participation.

    Regards,
    Ricky

  8. #8
    Registered User
    Join Date
    05-02-2006
    Posts
    95

    Re: For Each Loop (Make it Faster)

    Quote Originally Posted by JimBobBowie View Post
    Depending on the version of excel you are using the simplest thing you can do to speed things up is to turn off auto calculation while running this macro. You can do this manually or via code. Currently each time you hide a row the sheet will recalculate. This takes a long time.


    Please Login or Register  to view this content.
    JimBobBowie
    Thank-you JimBobBowie,
    Yes, I have trying turning off calculation and screenupdating also. But it's still slow. I think it's because the code inherently is slow because it goes down the column one cell at a time and evaluates whether or not it's a "Hide".

    Regards,
    Ricky

  9. #9
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: For Each Loop (Make it Faster)

    Try this

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    05-02-2006
    Posts
    95

    Re: For Each Loop (Make it Faster)

    Quote Originally Posted by sweep View Post
    Try this

    Please Login or Register  to view this content.
    Thank-you sweep!
    I have tested the code without any alterations but it hid the entire range (all rows to 30,0000). Could you please assist?

    I appreciate it very much.

    Regards,
    Ricky

  11. #11
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: For Each Loop (Make it Faster)

    Check this out and see if you think it would help you.

    The offset and resize is with the assumption that you have a header row.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    05-02-2006
    Posts
    95

    Re: For Each Loop (Make it Faster)

    Thank-you skywriter,
    It works nicely.

    Looks like filtering is the way to go, also as suggested by sweep. I'm glad your code turns off the filtering once all of the "Hide" identifier rows are hidden. That way, my data query to another application would still work when refreshing my Hidden rows.

    Offset (1) means down one row. But what's the Count - 1 do? I plan on naming the ranges in case future users may insert rows or columns befor "A1". So I wonder if I could just leave this line code as is.

    Please Login or Register  to view this content.
    Thanks very much.

    Regards,
    Ricky

  13. #13
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: For Each Loop (Make it Faster)

    Current region ends at the last row with data. If we offset the current region one row we are now also including the blank row below that last row. By re-sizing the range to be one less than the original row count we are now excluding that blank row.

  14. #14
    Registered User
    Join Date
    05-02-2006
    Posts
    95

    Re: For Each Loop (Make it Faster)

    Thank-you skywriter.
    Makes perfect sense.

    Regards,
    Ricky

  15. #15
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: For Each Loop (Make it Faster)

    You're welcome.

+ 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] faster way to loop
    By mattress58 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-22-2015, 12:09 PM
  2. [SOLVED] Make VBA Faster
    By icondor517 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-29-2013, 07:42 AM
  3. Is it possible to get rid of this loop to make the code faster?
    By mhw1129 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-09-2011, 09:03 AM
  4. Make loop run faster
    By losmi8 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-20-2010, 01:11 AM
  5. Making a simple loop faster
    By gummi in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-23-2008, 07:07 PM
  6. can you make this faster?
    By John in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-25-2005, 04:05 PM
  7. [SOLVED] Faster For-Next Loop?
    By dgp@dodgeit.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-07-2005, 06:06 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