dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,362
- Office Version
- 365
- 2016
- Platform
- Windows
I have a spreadsheet that calculates costs over a period of time. A button can be pressed that copies the layout below it for data entry of a separate period. I have some code that works correctly to hide rows that have either a 0 or nothing in certain cells for the first period. The problem is that the button can be pressed x number of times but the hide rows code only hides the rows from the first period, it will not hide any rows for additional periods. Could someone please tell me how to change it so it will hide the rows for every period that is in the sheet.
I thought you might need the add rows code as well.
This is my code to hide the rows.
Thanks
I thought you might need the add rows code as well.
VBA Code:
Sub AddRows()
Dim WS As Worksheet, lr As Long, x As Long, AfterCopy As Long, lRow As Long
Set WS = ThisWorkbook.Worksheets("ACA_Quoting")
Call Move_Shape
Call InsertExtras
lRow = Cells(Rows.Count, "H").End(xlUp).Row 'Defines lRow as last used cell in column H
If lRow = 32 Then
x = 15
Else
x = 14 'The difference in values for x takes into account the extra 2 rows on the first page
End If '....that are not needed on pages there after. CaseWorker and Organisation
Call ClearExpenses
With WS
'lr = .Range("D" & Rows.Count).End(xlUp).Row
.Range("A8:V32").Copy .Range("A" & Rows.Count).End(xlUp).Offset(2, 0) 'Pastes a copy of the table below current table between the bottom of the table and the totals
.Range("A" & Rows.Count).End(xlUp).Offset(15, 0).PageBreak = xlPageBreakManual 'Inserts a page break
.Range("I40").Copy Range("I" & Rows.Count).End(xlUp).Offset(x, 0) 'Copies the page number function to the newly created page
End With
End Sub
This is my code to hide the rows.
VBA Code:
Sub HideRows()
Dim cell As Range, n As Long, i As Integer, e As Long
e = 0
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
i = 0
For Each cell In Range("D14:D17")
If cell.Value = "" Or cell.Value = 0 Then
i = i + 1
End If
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
Thanks