+ Reply to Thread
Results 1 to 10 of 10

VBA optimization with a "For Each"

  1. #1
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    VBA optimization with a "For Each"

    VBA optimization with a "For Each"

    I want to check rows 50,000 and greater for some cell background color on a 60,000 row sheet. Right now I essentially go
    Please Login or Register  to view this content.
    Now it actually spins for a couple of seconds just getting up to line 50000. I'd like to improve that. I already see that
    dim wksht as worksheet
    and substituting wksht for Worksheets(i) is useful, since my belief is that the "high bound" of the For statement is reevaluated every single time.

    I could also put "With c" above the if statement, but the goal here is to speed the
    For Each
    If
    end if
    next
    construct so a "With" there would seem to actually slow the interpretive processor. (Side note: I'm not clear whether snail-paced VBA is actually "compiled." I suspect that the "debug/compile" option really is nothing but a syntax preprocessor. Elucidation welcome.)

    An obvious possibility is to instead of wksht.UsedRange define a range of the specified row, column A, to the end of the worksheet. I'm not sure how I'd code that; for one thing, it may not be on the active sheet, so ActiveCell.SpecialCells(xlLastCell) or ActiveCell.SpecialCells(xlLastCell).Row doesn't work per se. I'm interested in how it would be coded, yet note that iStartRow may be row 1 in some cases, not necessarily 50000. Any solution would need to be to optimize both situations.

    TIA for advice.

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: VBA optimization with a "For Each"

    There might be a faster way, but this should be better than what you had.

    Please Login or Register  to view this content.

  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: VBA optimization with a "For Each"

    A little tighter:
    Please Login or Register  to view this content.

    To add that into your worksheets scenario:
    Please Login or Register  to view this content.
    _________________
    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
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: VBA optimization with a "For Each"

    Steven: thanks so much. Happily, set rgLast = wksht.Range("A1").SpecialCells(xlCellTypeLastCell).row
    is valid when not referencing the active sheet, so that's handy.

    Can you really range from A1 to the end row? I had problems testing that. Or should I use IV a la

    Set rg = Range("A1" , "IV" & worksheets(2).Range("A1").SpecialCells(xlCellTypeLastCell).row)

    Or maybe I should use wksht.Range("A1").SpecialCells(xlCellTypeLastCell).column instead of "IV"
    but would I need the nasty conversion to alpha?

    How about range(cells(1,1), cells(a,b)) where a and b are the long expressions shown above?

    Thanks again.

  5. #5
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: VBA optimization with a "For Each"

    Ignore that "IV" rambling, Steven. You were actually directly referencing the last cell, not the lastcell's row. I misread, sorry. JBeaucaire's post got me thinking straight on that aspect - thanks for both of your suggestions.

  6. #6
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: VBA optimization with a "For Each"

    I suspect that repeated reevaluation of loop "prongs" - or one of them - is part of VBA's slowness. I started out with "for each c in usedrange" because that seemed to have the best chance of NOT being reevaluated. On the other hand, maybe "for each" never reevaluates. Thoughts on that are welcome.

    Also, I've long coded things like
    Please Login or Register  to view this content.
    in the belief that the "upper" bound of loops is always reevaluated and that the "lower" is not. I also work to simplify the upper bound in situations where reversing the parms and using Step doesn't do the job.

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

    Re: VBA optimization with a "For Each"

    I believe you're overthinking this whole issue. The FOR/NEXT syntax is designed to loop through a series without repeating. It's inherent.

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA optimization with a "For Each"

    the bounds are not re-evaluated. run
    Please Login or Register  to view this content.
    and see that it does not end up in an infinite loop. the slow speed compared to the new versions is simply that they aren't testing 50000 irrelevant rows.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  9. #9
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: VBA optimization with a "For Each"

    Quote Originally Posted by JBeaucaire View Post
    I believe you're overthinking this whole issue. The FOR/NEXT syntax is designed to loop through a series without repeating. It's inherent.
    Not sure what you mean by inherent. To really exaggerate
    Please Login or Register  to view this content.
    The above should *not* be run by anyone whose infinite loop flower is unspoiled. Okay, okay, this is cheating, warping the loop variable, not the bounds.

    I may get your point, because
    Please Login or Register  to view this content.
    apparently does NOT reevaluate, ergo is not an infinite loop. Testing that just now surprised me. Okay, maybe I'm too paranoid.

    BTW I'm not aiming to avoid infinite loops or reprocessing within the loops. I'm just musing whether loop bounds are reevaluated, in the interest of optimizing every gosh durn cycle that I can.
    Last edited by Oppressed1; 05-16-2012 at 05:01 PM.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA optimization with a "For Each"

    Changing the loop control variable inside the loop is a cardinal sin. There is never a good reason for it.
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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