I am running some VBA macros in my Excel spreadsheet to help me eliminate irrelevant data from view to then allow me to export/copy a subset of the overall set of data I have. My spreadsheet size is Columns(A:BN) and Rows(1:4693), which I would imagine is a decent size data set. Columns(A:G) will always remain visible. I'm only interested in hiding what is in Columns(H:BN).
First, If I want to keep the column in view, I set the column value to "Y", then I click the 'Hide Columns" button. I do this by attaching this subroutine to a button:
Sub Hidecolumn()
Dim p As Range
For Each p In Range("H1:BN1").Cells
If p.Value = "N" Then
p.EntireColumn.Hidden = True
End If
Next p
End Sub
This works perfectly.
I also have a "Show All Columns" button to have all of the hidden columns reappear, and I do this with:
Sub Unhidecolumn()
Dim p As Range
For Each p In Range("H1:BN1").Cells
If p.Value = "Y" Or p.Value = "N" Then
p.EntireColumn.Hidden = False
End If
Next p
End Sub
Now that I have the columns I want, now I need to hide all of the unwanted rows. Each cell will have a "Y" in it if that row-column relationship exist. It will be blank if there is no relationship. I want to hide all rows where all of the visible cells in the cell range are blank. ALL CELLS in the cell range must be blank.
Once I've hidden all of the unwanted rows, then I can copy the remaining/visible data and copy it to a new workbook to share with others, because they only need the data relevant to them.
Finally, just like I am able to unhide all of the hidden columns, I need to also be able to unhide all of the hidden rows.
In the end, I want 4 buttons on my sheet.
1. Hide Columns
2. Unhide Columns
3. Hide Rows
4. Unhide Rows
I already have the first 2, now I need the last 2. Please help.
UPDATE: SOLUTION - Thank you @K.Davis!
Sub HideRowsSecond()
Dim srcRng As Range, ws As Worksheet
Set ws = ActiveSheet
Set srcRng = ws.Rows("5:" & ws.Cells(ws.Rows.Count, 4).End(xlUp).Row)
Dim R As Range, hideRng As Range
For Each R In srcRng
If Application.CountA(R.Columns("H:BN").SpecialCells(xlCellTypeVisible)) = 0 Then
If hideRng Is Nothing Then
Set hideRng = R.EntireRow
Else
Set hideRng = Application.Union(hideRng, R.EntireRow)
End If
End If
Next R
If Not hideRng Is Nothing Then hideRng.EntireRow.Hidden = True
MsgBox ("Complete")
End Sub