Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88050

Slow Excel VBA Loop - Looking for improvement suggestions

$
0
0

I created a VBA macro in Excel linked to a button that hides or unhides specific rows.

I want the button to be able to hide/unhide rows 5 and 6, 10 and 11, 15 and 16, 20, 21, 25, 26, 30, 31, etc... up to row 400 roughly.

Below are two macros that I've used to achieve this, both work, though the top one is slightly faster than the bottom one.

Can anyone suggestions ways to improve this further?

Private Sub HidePlannedNEW_Click()

    Dim i As Long

    For i = 5 To 50 Step 4

        If Rows(i).Hidden = False Then
            Rows(i).Hidden = True
            i = i + 1
            Rows(i).Hidden = True

        ElseIf Rows(i).Hidden = True Then
            Rows(i).Hidden = False
            i = i + 1
            Rows(i).Hidden = False

        End If

    Next i

End Sub

(for this second macro to work I created a helper column with the letter 'P' in the rows I wanted to hide)

Private Sub HidePlannedOLD_Click()

    For Each i In ActiveSheet.Range("D4:D600")

        If i.EntireRow.Hidden = False And i.Value = "P" Then
             i.EntireRow.Hidden = True

        ElseIf i.EntireRow.Hidden = True And i.Value = "P" Then
             i.EntireRow.Hidden = False

        End If

    Next i

End Sub

Many thanks in advance!


Viewing all articles
Browse latest Browse all 88050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>