+ Reply to Thread
Results 1 to 10 of 10

Looping hide row Optimization problem

Hybrid View

  1. #1
    Registered User
    Join Date
    10-23-2012
    Location
    Washington, DC
    MS-Off Ver
    Excel 2010
    Posts
    43

    Looping hide row Optimization problem

    I am working with a fairly large dataset for which I am trying to develop the quickest code possible that will:
    1. Find the rows (from 10 to 16009) in the fourth column whose value =0
    2. Hide the entire row where this condition is true

    Here is the code I am working with, however it obviously very slow. I would appreciate any advice as to how to improve this or utilize a better method.

    Private Sub Worksheet_Activate()
        Application.ScreenUpdating = False
    
        Dim i As Integer
        Dim r As Integer
        Dim x As Variant
        Dim y As Variant
        x = Array(10)
        y = Array(16009)
    
        For i = 0 To 2
            For r = x(i) To y(i)
                If Cells(r, 4).Value = "0" Then
                    Rows(r).EntireRow.Hidden = True
                Else
                    Rows(r).EntireRow.Hidden = False
                End If
            Next r
        Next i
    
        Application.ScreenUpdating = True
        Application.Calculate
    End Sub

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Looping hide row Optimization problem

    This will be much faster:

    Sub RowHider()
    Dim rHide As Range: Set rHide = Nothing
    Cells.EntireRow.Hidden = False
    For i = 10 To 16009
        Set r = Cells(i, "D")
        If r.Value = 0 Then
            If rHide Is Nothing Then
                Set rHide = r
            Else
                Set rHide = Union(rHide, r)
            End If
        End If
    Next i
    
    If rHide Is Nothing Then
    Else
        rHide.EntireRow.Hidden = True
    End If
    End Sub
    The speed comes from only "hiding" once!
    Gary's Student

  3. #3
    Registered User
    Join Date
    10-23-2012
    Location
    Washington, DC
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Looping hide row Optimization problem

    Thank you, it works much better! I see what you did by only hiding once. Since I want this to update the sheet each time the user navigates to it, I put the code back under the Worksheet_Activate() Private sub- which should not affect speed from what I can observe.

    How might I add a popup msgbox that would tell the user to wait while the page is update? Is it possible to include some sort of "time left" notification in this msgbox?

    Thanks again.

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Looping hide row Optimization problem

    I would display a TextBox before the loop runs and remove it after the loop runs.

  5. #5
    Registered User
    Join Date
    10-23-2012
    Location
    Washington, DC
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Looping hide row Optimization problem

    Great thanks, I am experimenting with different ways of doing this but it is not a major issue.
    However relating to this row hide loop- I will need to add a filter to the dataset but it seems that the rows do not remain hidden whenever the data is filtered according to a column parameter and then reset back without the filter. Does applying/removing a filter always undo hidden rows? Is there any way to get around this?

  6. #6
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Looping hide row Optimization problem

    Is it possible that this approach might be faster?

    Sub Select_Select()
    
    
        Dim rngFind As Range
        Dim strValueToPick As String
        Dim rngPicked As Range
        Dim rngLook As Range
        Dim strFirstAddress As String
    
        Application.ScreenUpdating = False
    
         Columns("D:D").Select
        
        Set rngLook = Selection
        strValueToPick = "0"
        With rngLook
        
            Set rngFind = .Find(strValueToPick, .Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not rngFind Is Nothing Then
                strFirstAddress = rngFind.Address
                Set rngPicked = rngFind
                Do
                    Set rngPicked = Union(rngPicked, rngFind)
                    Set rngFind = .FindNext(rngFind)
                Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstAddress
            End If
        End With
        
        If Not rngPicked Is Nothing Then
            rngPicked.EntireRow.Hidden = True
        End If
    
    End Sub
    Last edited by mehmetcik; 04-09-2013 at 05:18 PM. Reason: Forgot to select column D

  7. #7
    Registered User
    Join Date
    10-23-2012
    Location
    Washington, DC
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Looping hide row Optimization problem

    Thank you for your solution Mehmetcik, however although it does run much quicker the code is not hiding the correct rows (those in column 4 which have a 0, between rows 10 and 16009). I think the issue with your solution lies in that it is selecting row 1 column 1 as the starting point- I am also hesitant about using Select as well as xlWhole which might seem to take longer to me, however please explain how this would be quicker.

  8. #8
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Looping hide row Optimization problem

    Hi

    I amended my code to only look at column D.

    The find function is in true vba code and will run faster then anything that has a loop.

    Try it and see which is faster.

    Do you have a timer programme?

  9. #9
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Looping hide row Optimization problem

    I forgot to select Column D.

    Sorry.

  10. #10
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Looping hide row Optimization problem

    escobf,

    Another way (assumes data starts in column A and row 9 is a header row):
    Private Sub Worksheet_Activate()
        
        Dim lCalc As XlCalculation
        
        With Application
            lCalc = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
        
        Intersect(Me.UsedRange, Me.Range("9:16009")).AutoFilter 4, "<>0", xlFilterValues
        
        With Application
            .Calculation = lCalc
            .ScreenUpdating = True
        End With
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

+ 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