Can anyone reduce my code?

Alisonh

New Member
Joined
Feb 21, 2019
Messages
3
Hi

I have a code that works perfectly however it is long and takes a while to run - can anyone help to shorten the code? The code basically looks at different sections and hides rows if all columns contain 0.
Code:
Sub HideRows()
Dim i As Long
Dim j As Long
Dim hide As Boolean
For i = 6 To 15
    hide = True
    For j = 3 To 56
            If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 19 To 20
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 24 To 32
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 36 To 67
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 73 To 75
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 79 To 80
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 84 To 87
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 91 To 96
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 102 To 112
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 116 To 117
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 121 To 124
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 128 To 131
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 135 To 140
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 144 To 161
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 165 To 169
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 173 To 179
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
For i = 183 To 190
    hide = True
    For j = 3 To 56
        If Cells(i, j).Value > 0 Then
            hide = False
            Exit For
        End If
    Next j
    Rows(i).Hidden = hide
Next i
End Sub
 
Last edited by a moderator:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You can use "WorksheetFunction.Sum" to have the sum of a range.
And if your data doesn't have negative number then you can use it like this:

Code:
If WorksheetFunction.Sum(Range("A1:C5")) > 0 then hide = False
 
Last edited:
Upvote 0
Code:
Sub HideRows()
Dim rws As Range
Set rws = Range("6:15,19:20,24:32, [COLOR=#ff0000]ETC[/COLOR] ") [COLOR=#ff0000]'Replace ETC with the rest of the row refs[/COLOR]
rws.EntireRow.Hidden = False
[A:A].Insert
With Intersect([A:A], rws)
    .Formula = "=IF(SUM(D6:BE6)=0,""h"",1)"
    On Error Resume Next
    .SpecialCells(xlCellTypeFormulas, 2).EntireRow.Hidden = True
    On Error GoTo 0
End With
[A:A].Delete
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top