VBA code to clear identical sections on multiple pages

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have tried to write some code for my spreadsheet. This spreadsheet has x number of pages. All pages except the first are identical. I already have some code to clear out the first two pages. I am having trouble with the code to clear out any pages past the third.

This code I have written is meant to find the bottom row on the sheet. From that row, check it and the 8 rows above it in column D. If there is nothing or a 0 in the cells, the rows get hidden. After those 8 rows, leave a gap of 2 blank rows then do the same thing for the 4 rows above those 2. There is then a gap of 25 rows above it until the bottom of the previous page where everything is the same.

It needs to keep doing this until row 65 is reached as at that is the bottom row from the second page and I have code that correctly works for 1 and 2 pages/periods.

This is my code but it doesn't work correctly. Could someone help me find what is wrong with it please?

VBA Code:
Sub ThirdNMore()
Dim cell As Range, e As Long
    e = Range("D" & Rows.Count).End(xlUp).Row
    Do Until e < 66
        For Each cell In Range("D" & e & ":D" & e - 8)
            If cell.Value = "" Or cell.Value = 0 Then Rows(e).Hidden = True
            e = e - 1
        Next cell
            e = e - 2
        For Each cell In Range("D" & e & ":D" & e - 3)
            If cell.Value = "" Or cell.Value = 0 Then Rows(e).Hidden = True
            e = e - 1
        Next cell
        
        e = e - 25
     Loop
End Sub

I have written some code that does work for having only 1 or 2 pages/periods. I tried however to run all of the code with 3 pages and row 92 and 93 do not get hidden if there is nothing or a zero in them. I am still very new to coding so some help would be appreciated.

This is the working code

VBA Code:
Sub HideRows()
Dim cell As Range, n As Long, e As Long
e = Range("D" & Rows.Count).End(xlUp).Row
    Select Case e
        Case 28
            Call FirstCopy
        Case 65
            Call FirstCopy
            Call SecondCopy
        Case Else
            Do Until e < 66
                Call FirstCopy
                Call SecondCopy
                Call ThirdNMore
                e = e - 39
            Loop
        End Select
End Sub

Sub FirstCopy()
Dim cell As Range, n As Long, i As Integer, e As Long, c As Long
e = Range("D" & Rows.Count).End(xlUp).Row
n = 14
i = 0
            For Each cell In Range("D14:D17")
                If cell.Value = "" Or cell.Value = 0 Then Rows(n).Hidden = True
                n = n + 1
                i = i + 1
            Next cell

            If i = 4 Then Rows(13).Hidden = True
n = 20
            For Each cell In Range("D20:D28")
                If cell.Value = "" Or cell.Value = 0 Then Rows(n).Hidden = True
                n = n + 1
            Next cell
End Sub

Sub SecondCopy()
Dim cell As Range, n As Long, e As Long
e = 65

    For Each cell In Range("D57:D65")
        If cell.Value = "" Or cell.Value = 0 Then Rows(e).Hidden = True
        e = e - 1
    Next cell
        e = e - 2
    For Each cell In Range("D51:D54")
        If cell.Value = "" Or cell.Value = 0 Then Rows(e).Hidden = True
        e = e - 1
    Next cell
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,214,967
Messages
6,122,503
Members
449,090
Latest member
RandomExceller01

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