The_Steward
Board Regular
- Joined
- Nov 26, 2020
- Messages
- 63
- Office Version
- 365
- Platform
- Windows
I have a spreadsheet that I enter data into for reports and the print out the data into a PDF report.
I have one sheet to enter all the data, and one sheet where I setup the layout for the PDF document
When a line is blank on the first sheet it will of be reflected in the print out, which means I have lots of empty cells/boxes in my PDF.
So I want to make the PDF document cleaner by removing the ones that are empty.
I have attached some photos to show how everything is layed out. As you can see in the 'calculator' sheet there are 22 supports, then in 'PDF layout' the data for each support is reformatted for the creation of the PDF document. When it reaches the data included in the 2nd table with 19 possible supports, the only formatting change in print out is the colour.
Below is the macro I have created so far, as you can see I tried concatenating the range string to accommodate for all the merged cells in the PDF layout. But what i'm really trying to do is hide rows 11-20 when C12,C15:L15,C18 are blank, rows 21-30 when C22,C25:L25,C28 are blank, rows 31-40 when cells C32,C35:L35,C38 are blank and so forth until I get to rows 411-420 - C412,C415:L415,C418. And the unhide them whenever one or more of the cells is not blank.
How can I edit the following Macro to make it work?
I have one sheet to enter all the data, and one sheet where I setup the layout for the PDF document
When a line is blank on the first sheet it will of be reflected in the print out, which means I have lots of empty cells/boxes in my PDF.
So I want to make the PDF document cleaner by removing the ones that are empty.
I have attached some photos to show how everything is layed out. As you can see in the 'calculator' sheet there are 22 supports, then in 'PDF layout' the data for each support is reformatted for the creation of the PDF document. When it reaches the data included in the 2nd table with 19 possible supports, the only formatting change in print out is the colour.
Below is the macro I have created so far, as you can see I tried concatenating the range string to accommodate for all the merged cells in the PDF layout. But what i'm really trying to do is hide rows 11-20 when C12,C15:L15,C18 are blank, rows 21-30 when C22,C25:L25,C28 are blank, rows 31-40 when cells C32,C35:L35,C38 are blank and so forth until I get to rows 411-420 - C412,C415:L415,C418. And the unhide them whenever one or more of the cells is not blank.
How can I edit the following Macro to make it work?
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
Dim rowStart As Long
Dim rowEnd As Long
Dim rangeStr As String
'Loop through the ranges of cells to check
For i = 1 To 21 Step 3
rowStart = (i - 1) * 10 + 1
rowEnd = rowStart + 9
rangeStr = "C" & rowStart + 1 & ":D" & rowStart + 2 & ", E" & rowStart + 1 & ":E" & rowStart + 2 & ", F" & rowStart + 1 & ":F" & rowStart + 2 & ", G" & rowStart + 1 & ":H" & rowStart + 2 & ", I" & rowStart + 1 & ":J" & rowStart + 2 & ", K" & rowStart + 1 & ":L" & rowStart + 2 & ", C" & rowStart + 7 & ", C" & rowStart + 12 & ":L" & rowStart + 13 & ", C" & rowStart + 17
'Check if the cells in the current range are blank
If Application.WorksheetFunction.CountIf(Range(rangeStr), "") = Range(rangeStr).Cells.Count Then
'Hide the rows in the current range
Rows(rowStart & ":" & rowEnd).EntireRow.Hidden = True
Else
'Unhide the rows in the current range
Rows(rowStart & ":" & rowEnd).EntireRow.Hidden = False
End If
Next i
End Sub