Hello, I've written code that hides rows on different sheets if they are empty and in one case changes column width to keep the formatting looking ok. I've used non-contiguous named ranges because there are multiple places on each row that could (or could not) be empty. It does exactly what I want, but it takes a long time to process and will only get longer if I continue to add new sheets. I'm pretty new to VBA, but I know I didn't write it well as I am iterating through multiple ranges and performing tests on each cell. I'm pretty sure I could use an array, but I failed at coding that myself. Moreover, using xlspecialcellblanks isn't working, because the "empty" cells have formulas in them. Any help would be greatly appreciated! Thanks very much.
Sub F2F4HideRows()
Application.ScreenUpdating = False
Dim rng As Range
Sheets("F2").Range("F2HideRows").Rows.EntireRow.Hidden = True
Sheets("F4").Range("F4HideRows").Rows.EntireRow.Hidden = True
Sheets("F5").Range("F5HideRows").Rows.EntireRow.Hidden = True
Sheets("F6").Range("F6HideRows").Rows.EntireRow.Hidden = True
Sheets("F4").Columns("C").ColumnWidth = 17
Sheets("F4").Columns("D").ColumnWidth = 17
Sheets("F4").Columns("F").ColumnWidth = 17
On Error Resume Next
For Each rng In Sheets("F2").Range("F2HideRows")
If rng <> Empty Then
rng.Rows.EntireRow.Hidden = False
End If
Next
For Each rng In Sheets("F4").Range("F4HideRows")
If rng <> Empty Then
rng.Rows.EntireRow.Hidden = False
Sheets("F4").Columns("C").ColumnWidth = Sheets("F4").Columns("C").ColumnWidth + 0.3
Sheets("F4").Columns("D").ColumnWidth = Sheets("F4").Columns("D").ColumnWidth + 0.3
Sheets("F4").Columns("F").ColumnWidth = Sheets("F4").Columns("F").ColumnWidth + 0.3
End If
Next
For Each rng In Sheets("F5").Range("F5HideRows")
If rng <> Empty Then
rng.Rows.EntireRow.Hidden = False
End If
Next
For Each rng In Sheets("F6").Range("F6HideRows")
If rng <> Empty Then
rng.Rows.EntireRow.Hidden = False
End If
Next
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
Bookmarks