Hide and unhide rows based on empty cells

The_Steward

Board Regular
Joined
Nov 26, 2020
Messages
63
Office Version
  1. 365
Platform
  1. 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.
Screenshot 2023-03-20 173701.png
Screenshot 2023-03-20 173752.png
Screenshot 2023-03-20 173908.png



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
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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