dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,352
- Office Version
- 365
- 2016
- Platform
- 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?
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
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