+ Reply to Thread
Results 1 to 12 of 12

Speed Up Macro that Hides Rows

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Tampa, FL
    MS-Off Ver
    Excel 2010
    Posts
    278

    Speed Up Macro that Hides Rows

    Hi - this macro works but it seems slow. Any suggestions for speeding it up? It's attached to a checkbox.

    Application.ScreenUpdating = False
    
        With ThisWorkbook.ActiveSheet
        
            Dim i As Long
            Dim lr As Long
            lr = Cells(Rows.Count, 1).End(xlUp).Row
            
            For i = 1 To lr
                If Range("D" & i).Interior.Color = RGB(217, 217, 217) Then
                    Range("D" & i).EntireRow.Hidden = True
                End If
            Next
         
        End With
    
    Application.ScreenUpdating = True
    It also doesn't take into consideration any rows that might be hidden already, however, that shouldn't happen but probably could.
    I have the same basic code to unhide the rows.

    Thank You.

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Speed Up Macro that Hides Rows

    I've never really tested this, but when I deleted or hide rows I usually do this

    For i = lr to 1 step - 1
    I don't know if it would be faster....but hey, it's worth a shot...

    HTH
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Tampa, FL
    MS-Off Ver
    Excel 2010
    Posts
    278

    Re: Speed Up Macro that Hides Rows

    Hi JudgeH59 - I tried your suggestion but didn't notice any appreciable increase or decrease in speed. Thank you for trying!
    Last edited by ker9; 07-24-2015 at 02:12 PM.

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Speed Up Macro that Hides Rows

    Autofilter will probably be one of the fastest options. Assuming autofilter is an acceptable solution for "hiding" the rows.

    Sub AutoFilterbyColors()
    Dim ws As Worksheet:    Set ws = ActiveSheet
    
    With ws
        .AutoFilterMode = False
        .Range("D1:D" & .Range("D" & Rows.Count).End(xlUp).Row).AutoFilter 1, RGB(217, 217, 217), xlFilterCellColor
    End With
    
    End Sub
    Last edited by stnkynts; 07-24-2015 at 01:05 PM.
    If you are happy with my response please click the * in the lower left of my post.

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Speed Up Macro that Hides Rows

    Are you referring to my suggestion? Our posts were about the same time

  6. #6
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Tampa, FL
    MS-Off Ver
    Excel 2010
    Posts
    278

    Re: Speed Up Macro that Hides Rows

    Hi stnkynts - thank you for your response, but autofilter won't work for this application.

  7. #7
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Speed Up Macro that Hides Rows

    so a few question that are standard for "Slow" stuff

    1) do you run conditional formatting?....that can really slow stuff down
    2) are you doing a lot of calculations?....like Array formulas
    3) how long is it actually taking?....how many rows and columns in the worksheet.,
    4) how many worksheets?

  8. #8
    Valued Forum Contributor
    Join Date
    02-06-2014
    Location
    N/A
    MS-Off Ver
    N/A
    Posts
    373

    Re: Speed Up Macro that Hides Rows

    Hello! Try this.

    Sub Hide_Row()
    
        Application.ScreenUpdating = False
    
        With ThisWorkbook.ActiveSheet
        
            Dim i As Long
            Dim lr As Long
            lr = Cells(Rows.Count, 1).End(xlUp).Row
            
            Debug.Print lr
            
            Dim rRange As Range
            
            For i = 1 To lr
                
                If Range("D" & i).Interior.Color = RGB(217, 217, 217) Then
                    
                    If rRange Is Nothing Then
                    
                        Set rRange = Range("D" & i).EntireRow
                    
                    Else
                        
                        Set rRange = Union(rRange, Range("D" & i).EntireRow)
                    
                    End If
                
                End If
                
            Next
         
        End With
        
        rRange.EntireRow.Hidden = True
    
        Application.ScreenUpdating = True
    
    End Sub

  9. #9
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Tampa, FL
    MS-Off Ver
    Excel 2010
    Posts
    278

    Re: Speed Up Macro that Hides Rows

    Poizhan: WOW!! - that is a lot faster! Not sure I understand why - perhaps you can explain? THANK YOU!

    judgeH59: +/- 300 rows and 5 columns, no conditional formatting and no calculations - it's all text.

    All: after struggling with this I decided to shut down everything and restart the PC to ensure there wasn't anything hogging memory. After doing that, everything seemed to work faster but Poizhan's answer is faster yet. Thank you!

  10. #10
    Valued Forum Contributor
    Join Date
    02-06-2014
    Location
    N/A
    MS-Off Ver
    N/A
    Posts
    373

    Re: Speed Up Macro that Hides Rows

    Quote Originally Posted by ker9 View Post
    Poizhan: WOW!! - that is a lot faster! Not sure I understand why - perhaps you can explain? THANK YOU!

    judgeH59: +/- 300 rows and 5 columns, no conditional formatting and no calculations - it's all text.

    All: after struggling with this I decided to shut down everything and restart the PC to ensure there wasn't anything hogging memory. After doing that, everything seemed to work faster but Poizhan's answer is faster yet. Thank you!
    While iterating, instead of each pass performing the hide action (this is what causes the overhead) by row, you can create an instance of a range object, on the first pass that meets the IF criteria, you will set it to that range, on each subsequent pass that meets the IF criteria, you will Union that range to the existing range (analogous to creating an array of range pointers) and then once your FOR loop is finished, you perform the hide on the entire range in one action.

  11. #11
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Speed Up Macro that Hides Rows

    sweet....glad someone was able to get it going faster....

  12. #12
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Speed Up Macro that Hides Rows

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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. Need macro that hides rows based on the result of an equation.
    By fletch77776 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-12-2014, 10:54 PM
  2. Macro hides the wrong rows and is too slow
    By Marvelous in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-30-2014, 05:01 PM
  3. [SOLVED] Worksheet Macro that hides empty rows does not work
    By Taktiker in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-22-2012, 11:11 AM
  4. [SOLVED] Macro the hides entire rows based on a drop down list
    By MGK086 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-14-2012, 11:32 AM
  5. [SOLVED] Macro code that hides or unhides rows in other worksheets
    By nenadmail in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-25-2012, 04:10 PM
  6. How to make my macro delete rows? It currently hides the rows instead of deleting.
    By Majkataxmk in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 05-20-2010, 01:14 PM
  7. macro that hides rows run slow
    By yoav_b in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-15-2007, 11:16 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