+ Reply to Thread
Results 1 to 7 of 7

Optimizing code speed: Hiding rows with loops - looking for faster way

Hybrid View

Taktiker Optimizing code speed: Hiding... 10-24-2012, 06:16 PM
Norie Re: Optimizing code speed:... 10-24-2012, 06:28 PM
Taktiker Re: Optimizing code speed:... 10-27-2012, 02:56 PM
Taktiker Re: Optimizing code speed:... 10-27-2012, 03:04 PM
Taktiker Re: Optimizing code speed:... 10-27-2012, 05:08 PM
SuitedAces Re: Optimizing code speed:... 11-04-2012, 11:15 PM
Taktiker Re: Optimizing code speed:... 11-07-2012, 04:24 PM
  1. #1
    Registered User
    Join Date
    08-19-2012
    Location
    Germany
    MS-Off Ver
    Excel 2010
    Posts
    50

    Optimizing code speed: Hiding rows with loops - looking for faster way

    Hello!
    The following code hid the rows of the listed cells if the cell does not contain anything if I click on the checkbox in my sheet and unhides them if I click again. The problem is that it needs about 5 to 7 seconds for the rows to disappear and I would like to have it faster. Does anyone have an idea how to get the same result faster? Without that many loop stages?
    If Range("B63").Value = True Then
                Set myRng = Range("C4,C5,C6,C7,C8,C9,C10,C12,C13,C14,C15,C16,C17,C18,C19,C21,C22,C23,C24,C25,C26,C28,C29,C30,C31,C32,C33,C34,C35,C37,C38,C40,C41,C42,C43,C44,C45,C46,C48,C49,C51,C52,C53,C54,C55,C56,C57,C59,C60,C61")
                For Each myCell In myRng.Cells
                    If myCell.Value = "" Then
                    myCell.EntireRow.Hidden = True
                    End If
                Next myCell
    Else
    Range("C1:C65536").EntireRow.Hidden = False
    Greetings,
    Taktiker
    Last edited by Taktiker; 11-07-2012 at 04:24 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Optimizing code speed: Hiding rows with loops - looking for faster way

    Try this.
    Dim myRng As Range
    Dim myCell As Range
    Dim rngHide As Range
    
        If Range("B63").Value = True Then
            Set myRng = Range("C4,C5,C6,C7,C8,C9,C10,C12,C13,C14,C15,C16,C17,C18,C19,C21,C22,C23,C24,C25,C26,C28,C29,C30,C31,C32,C33,C34,C35,C37,C38,C40,C41,C42,C43,C44,C45,C46,C48,C49,C51,C52,C53,C54,C55,C56,C57,C59,C60,C61")
            For Each myCell In myRng.Cells
    
                If myCell.Value = "" Then
                    If rngHide Is Nothing Then
                        Set rngHide = myCell
                    Else
                        Set rngHide = Union(rngHide, myCell.EntireRow)
                    End If
                End If
            Next myCell
        Else
            Range("C1:C65536").EntireRow.Hidden = False
        End If
    
        If Not rngHide Is Nothing Then
    
            rngHide.EntireRow.Hidden = True
        End If

  3. #3
    Registered User
    Join Date
    08-19-2012
    Location
    Germany
    MS-Off Ver
    Excel 2010
    Posts
    50

    Re: Optimizing code speed: Hiding rows with loops - looking for faster way

    My this is awesome! From 6-7 seconds to 0,1 seconds. Unbelievable. Thanks

  4. #4
    Registered User
    Join Date
    08-19-2012
    Location
    Germany
    MS-Off Ver
    Excel 2010
    Posts
    50

    Re: Optimizing code speed: Hiding rows with loops - looking for faster way

    Actually, I have another code: Its the second rowhiding and is intended to hide the capture-rows. It is essentially a similar code, but a bit different. Dont be scared by its size, its just the same 9 times for 9 rows. I didn't see a way to use the union thing, so my question is: Is there a way to optimize it? So its so super fast, too?
    Sub Diätplaner_Druckansicht_erweitert()
    Dim myRng As Range
    Dim myCell As Range
    Dim Cell1 As Range
    Dim Range1 As Range
    Dim Hidden1 As Boolean
    Dim Cell2 As Range
    Dim Range2 As Range
    Dim Hidden2 As Boolean
    Dim Cell3 As Range
    Dim Range3 As Range
    Dim Hidden3 As Boolean
    Dim Cell4 As Range
    Dim Range4 As Range
    Dim Hidden4 As Boolean
    Dim Cell5 As Range
    Dim Range5 As Range
    Dim Hidden5 As Boolean
    Dim Cell6 As Range
    Dim Range6 As Range
    Dim Hidden6 As Boolean
    Dim Cell7 As Range
    Dim Range7 As Range
    Dim Hidden7 As Boolean
    Dim Cell8 As Range
    Dim Range8 As Range
    Dim Hidden8 As Boolean
    Dim Cell9 As Range
    Dim Range9 As Range
    Dim Hidden9 As Boolean
            
            If Range("B64").Value = True Then
            Set Range1 = Range("C4:C10")
            For Each Cell1 In Range1.Cells
                If Cell1.EntireRow.Hidden = True Then
                    Hidden1 = True
                Else: Hidden1 = False
                Exit For
                End If
            Next Cell1
            If Hidden1 = True Then Range("C3").EntireRow.Hidden = True
            
            
            Set Range2 = Range("C12:C19")
            For Each Cell2 In Range2.Cells
                If Cell2.EntireRow.Hidden = True Then
                    Hidden2 = True
                Else: Hidden2 = False
                Exit For
                End If
            Next Cell2
            If Hidden2 = True Then Range("C11").EntireRow.Hidden = True
            
            Set Range3 = Range("C21:C26")
            For Each Cell3 In Range3.Cells
                If Cell3.EntireRow.Hidden = True Then
                    Hidden3 = True
                Else: Hidden3 = False
                Exit For
                End If
            Next Cell3
            If Hidden3 = True Then Range("C20").EntireRow.Hidden = True
    
            Set Range4 = Range("C28:C35")
            For Each Cell4 In Range4.Cells
                If Cell4.EntireRow.Hidden = True Then
                    Hidden4 = True
                Else: Hidden4 = False
                Exit For
                End If
            Next Cell4
            If Hidden4 = True Then Range("C27").EntireRow.Hidden = True
            
            Set Range5 = Range("C37:C38")
            For Each Cell5 In Range5.Cells
                If Cell5.EntireRow.Hidden = True Then
                    Hidden5 = True
                Else: Hidden4 = False
                Exit For
                End If
            Next Cell5
            If Hidden5 = True Then Range("C36").EntireRow.Hidden = True
            
            Set Range6 = Range("C40:C46")
            For Each Cell6 In Range6.Cells
                If Cell6.EntireRow.Hidden = True Then
                    Hidden6 = True
                Else: Hidden6 = False
                Exit For
                End If
            Next Cell6
            If Hidden6 = True Then Range("C39").EntireRow.Hidden = True
            
            Set Range7 = Range("C48:C49")
            For Each Cell7 In Range7.Cells
                If Cell7.EntireRow.Hidden = True Then
                    Hidden7 = True
                Else: Hidden7 = False
                Exit For
                End If
            Next Cell7
            If Hidden7 = True Then Range("C47").EntireRow.Hidden = True
            
            Set Range8 = Range("C51:C57")
            For Each Cell8 In Range8.Cells
                If Cell8.EntireRow.Hidden = True Then
                    Hidden8 = True
                Else: Hidden8 = False
                Exit For
                End If
            Next Cell8
            If Hidden8 = True Then Range("C50").EntireRow.Hidden = True
            
            Set Range9 = Range("C59:C61")
            For Each Cell9 In Range9.Cells
                If Cell9.EntireRow.Hidden = True Then
                    Hidden9 = True
                Else: Hidden9 = False
                Exit For
                End If
            Next Cell9
            If Hidden9 = True Then Range("C58").EntireRow.Hidden = True
        Else
        Range("C3,C11,C20,C27,C39,C50,C58").EntireRow.Hidden = False
        If Range("E67").Value = "True" Then Range("C36,C47").EntireRow.Hidden = False
        End If
    End Sub

  5. #5
    Registered User
    Join Date
    08-19-2012
    Location
    Germany
    MS-Off Ver
    Excel 2010
    Posts
    50

    Re: Optimizing code speed: Hiding rows with loops - looking for faster way

    bump, bc its already on the end of page 2

  6. #6
    Forum Contributor
    Join Date
    06-27-2006
    Posts
    310

    Re: Optimizing code speed: Hiding rows with loops - looking for faster way

    You could name the ranges to simplify your code.
    For example...
    Sub SetRangeNames()
    Range("C4:10").Name = "rng_1_1"
    Range("C12:C19").Name = "rng_1_2"
    Range("C21:C26").Name = "rng_1_3"
    Range("C28:C35").Name = "rng_1_4"
    Range("C37:C38").Name = "rng_1_5"
    Range("C40:C46").Name = "rng_1_6"
    Range("C48:C49").Name = "rng_1_7"
    Range("C51:C57").Name = "rng_1_8"
    Range("C59:C61").Name = "rng_1_9"
    Range("C3").Name = "rng_2_1"
    Range("C11").Name = "rng_2_2"
    Range("C20").Name = "rng_2_3"
    Range("C27").Name = "rng_2_4"
    Range("C36").Name = "rng_2_5" '
    Range("C39").Name = "rng_2_6"
    Range("C47").Name = "rng_2_7" '
    Range("C50").Name = "rng_2_8"
    Range("C58").Name = "rng_2_9"
    Range("C3,C11,C20,C27,C39,C50,C58").Name = "group1"
    Range("C36,C47").Name = "group2"
    End Sub
    You can delete it after you run it

    Then reduce your original code to...
    Sub Diätplaner_Druckansicht_erweitert()
    Dim i As Integer
    Application.ScreenUpdating = False
    If Range("B64").Value = True Then
        For i = 1 To 9
           If Range("rng_1_" & i).EntireRow.hidden Then Range("rng_2_" & i).EntireRow.hidden = True
        Next i
    Else
        Range("group1").EntireRow.hidden = False
        If Range("E67").Value = "True" Then Range("group2").EntireRow.hidden = False
    End If
    Application.ScreenUpdating = True
    End Sub
    Let me know if it works as intended, I only did a partial test
    Last edited by SuitedAces; 11-04-2012 at 11:19 PM.

  7. #7
    Registered User
    Join Date
    08-19-2012
    Location
    Germany
    MS-Off Ver
    Excel 2010
    Posts
    50

    Re: Optimizing code speed: Hiding rows with loops - looking for faster way

    Application.ScreenUpdating = False & True already fixed the problem. It does essentially what I wanted, not showing the calculationstuff and needs less time. Thx!

+ 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