+ Reply to Thread
Results 1 to 7 of 7

Loop through visible cells only

  1. #1
    Registered User
    Join Date
    04-04-2018
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    15

    Smile Loop through visible cells only

    [FORMULA]Please Login or Register to view this content.[/code]
    Last edited by zanac; 01-22-2020 at 07:15 PM. Reason: adding programming code

  2. #2
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Loop through visible cells only

    Any time VBA macros seem to take a very long time to run, it's likely best to consider adding the following code.

    Please Login or Register  to view this content.
    See if that doesn't speed up your code.

    Anyway, in code, you can check whether the kth row of range rng isn't hidden due to a filter using

    If Application.WorksheetFunction.Subtotal(103, rng.Rows(k)) > 0 Then . . .

    Note: the function will return 0 if row k is hidden due to a filter OR if all cells in that row are blank and it's not hidden by the filter. It'll return a positive number if the row is visible (passed through the filter) AND contains at least one nonblank cell.

  3. #3
    Registered User
    Join Date
    01-04-2020
    Location
    Hull, UK
    MS-Off Ver
    2016
    Posts
    60

    Re: Loop through visible cells only

    You can use Range.SpecialCells(xlCellTypeVisible) to just loop over the visible cells

    Something like this should do it...
    Please Login or Register  to view this content.
    Last edited by juddaaaa; 01-22-2020 at 08:27 PM.

  4. #4
    Registered User
    Join Date
    04-04-2018
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    15

    Re: Loop through visible cells only

    Thank you hrlngrv, that helps to reduce the time little bit
    Last edited by zanac; 01-29-2020 at 03:22 PM.

  5. #5
    Registered User
    Join Date
    04-04-2018
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    15

    Re: Loop through visible cells only

    Quote Originally Posted by hrlngrv View Post
    Any time VBA macros seem to take a very long time to run, it's likely best to consider adding the following code.

    Please Login or Register  to view this content.
    See if that doesn't speed up your code.

    Anyway, in code, you can check whether the kth row of range rng isn't hidden due to a filter using

    If Application.WorksheetFunction.Subtotal(103, rng.Rows(k)) > 0 Then . . .

    Note: the function will return 0 if row k is hidden due to a filter OR if all cells in that row are blank and it's not hidden by the filter. It'll return a positive number if the row is visible (passed through the filter) AND contains at least one nonblank cell.
    Thank you hrlngrv, that helps to reduce the time little bit

  6. #6
    Registered User
    Join Date
    04-04-2018
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    15

    Re: Loop through visible cells only

    Quote Originally Posted by juddaaaa View Post
    You can use Range.SpecialCells(xlCellTypeVisible) to just loop over the visible cells

    Something like this should do it...
    Please Login or Register  to view this content.
    That actually worked perfectly!! thank you so much Juddaaaa!!

  7. #7
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Loop through visible cells only

    I was hoping not to be tempted to rewrite this, but oh well.

    Please Login or Register  to view this content.
    The main idea here is that it's most efficient to load worksheet range data into VBA arrays in as few VBA operations as possible, and likewise return as much data from VBA arrays to worksheet ranges in as few VBA operations as possible.

    In this case, I load the values of E2:En into arr1 and R2:Rn into arr2 in single VBA statements, then process those arrays, then return the values in arr2 to R2:Rn in a single VBA statement. Then I clear arr1 and load it with the values from P2:Pn, and continue processing arr2, which again gets returned to R2:Rn.

    If you need P2:Pn or R2:Rn to recalc based on the results of the 1st For loop, you need the rngAll.Columns(18).Value2 = arr2 statement between the 1st and 2nd For loops. If P2:Pn and R2:Rn are all constants, then you could remove that statement.

    The only operation inside any of the For loops which needs to be done to worksheet cells directly is setting the Interior (cell background) color. Note, however, that color is uninitialized within this procedure. Is it a global variable? If not, learn to start all VBA modules with Option Explicit.

    Final thing to note, your original code's 2nd and 3rd For loops were

    Please Login or Register  to view this content.
    which meant that you were redundantly processing rows. That is, if C = 2, 3 and 4 were all visible, you would have compared rows D = 3 and 4 to row C = 2, so there was no reason to compare row D = 2 to row C = 3 or 4.

+ 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. Loop through visible cells and every x..th cell add page break.
    By tuc47285 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-05-2015, 05:47 PM
  2. Colour visible cells in a loop VBA
    By hawo12 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-23-2014, 02:06 PM
  3. Adjust Code to Loop through Visible(filtered) cells only
    By enidnunez in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-19-2013, 09:23 AM
  4. [SOLVED] Copy/Pasting Values from visible cells (using autofilter) to visible cells
    By evakotsi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2012, 07:49 AM
  5. [SOLVED] loop on visible cells only
    By Sliman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-26-2006, 09:10 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