Excel VBA to hide rows based on cell value across multiple sheets

Stuepef

Board Regular
Joined
Oct 23, 2017
Messages
128
Office Version
  1. 2021
Platform
  1. Windows
I am attempting to write excel VBA to hide blank rows across multiple worksheets based on cell value in Column A. Sheet 1 is where I use formulas to populate the rest of the sheets so this sheet will need to be excluded from the VBA. Sheet 2-5 are identical in terms of last names displayed and the exact same range/# of empty rows. Sheets 6-8 will look identical to sheets 2-5, but have a different range that will need blank rows hidden.

Sheet 2-5 Range A9-A58:
Column A
Ross
Kirby
George
Stevens
Sears
(blank)
(blank)
(Blank)

Sheet 6-8 Range A16-A65:
Column A
Ross
Kirby
George
Stevens
Sears
(blank)
(blank)
(Blank)

My current VBA code:
Code:
Sheets(array("Sheet 2", "Sheet 3", "Sheet 4", Sheet 5"))
Dim aStart as Long,
Dim aStop as Long,
aStart = 19
aStop = 58
Dim i as Long,
For i = aStart to aStop
If Cells( i, "A").Value = "" Then
Rows(i)EntireRow.Hidden = True
End If
Next i
End Sub

The code works only for the active sheet I run the code on and not the other sheets listed. Also, I am not sure where to add the code for Sheets 6-8 with the different range than sheets 2-5. Any help is greatly appreciated!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi Stuepef,

Welcome to MrExcel!!

This should do the job for you:

Code:
Option Explicit
Sub Macro1()

    Dim wsMySheet As Worksheet
    Dim lngMyRow  As Long
    
    Application.ScreenUpdating = False
    
    For Each wsMySheet In ThisWorkbook.Sheets
        Select Case wsMySheet.Name
            Case Is = "Sheet2", "Sheet3", "Sheet4", "Sheet5"
                For lngMyRow = 58 To 9 Step -1 'Need to work backwards through the rows when hiding or deleting
                    If Len(wsMySheet.Range("A" & lngMyRow)) = 0 Then
                        wsMySheet.Range("A" & lngMyRow).EntireRow.Hidden = True
                    Else
                        wsMySheet.Range("A" & lngMyRow).EntireRow.Hidden = False
                    End If
                Next lngMyRow
            Case Is = "Sheet6", "Sheet7", "Sheet8"
                For lngMyRow = 65 To 16 Step -1 'Need to work backwards through the rows when hiding or deleting
                    If Len(wsMySheet.Range("A" & lngMyRow)) = 0 Then
                        wsMySheet.Range("A" & lngMyRow).EntireRow.Hidden = True
                    Else
                        wsMySheet.Range("A" & lngMyRow).EntireRow.Hidden = False
                    End If
                Next lngMyRow
        End Select
    Next wsMySheet
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Hi Stuepef,

Welcome to MrExcel!!

This should do the job for you:

Code:
Option Explicit
Sub Macro1()

    Dim wsMySheet As Worksheet
    Dim lngMyRow  As Long
    
    Application.ScreenUpdating = False
    
    For Each wsMySheet In ThisWorkbook.Sheets
        Select Case wsMySheet.Name
            Case Is = "Sheet2", "Sheet3", "Sheet4", "Sheet5"
                For lngMyRow = 58 To 9 Step -1 'Need to work backwards through the rows when hiding or deleting
                    If Len(wsMySheet.Range("A" & lngMyRow)) = 0 Then
                        wsMySheet.Range("A" & lngMyRow).EntireRow.Hidden = True
                    Else
                        wsMySheet.Range("A" & lngMyRow).EntireRow.Hidden = False
                    End If
                Next lngMyRow
            Case Is = "Sheet6", "Sheet7", "Sheet8"
                For lngMyRow = 65 To 16 Step -1 'Need to work backwards through the rows when hiding or deleting
                    If Len(wsMySheet.Range("A" & lngMyRow)) = 0 Then
                        wsMySheet.Range("A" & lngMyRow).EntireRow.Hidden = True
                    Else
                        wsMySheet.Range("A" & lngMyRow).EntireRow.Hidden = False
                    End If
                Next lngMyRow
        End Select
    Next wsMySheet
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert

Thank you for welcoming to me to MrExcel! This code works perfectly, thank you so much Robert!
 
Upvote 0
Thank you so much, Robert! This code works like a charm! Is there a way to unhide the same ranges that were hidden in the code above with a separate VBA?
 
Upvote 0
Thank you so much, Robert! This code works like a charm!

You're welcome.

Is there a way to unhide the same ranges that were hidden in the code above with a separate VBA?

The code will automatically unhide any row in Col. A that has a value but if there's no formula in these rows that may populate try this:

Code:
Option Explicit
Sub Macro2()

    Dim wsMySheet As Worksheet
    Dim lngMyRow  As Long
    
    Application.ScreenUpdating = False
    
    For Each wsMySheet In ThisWorkbook.Sheets
        Select Case wsMySheet.Name
            Case Is = "Sheet2", "Sheet3", "Sheet4", "Sheet5"
                For lngMyRow = 58 To 9 Step -1 'Need to work backwards through the rows when hiding or deleting
                    If Len(wsMySheet.Range("A" & lngMyRow)) = 0 Then
                        wsMySheet.Range("A" & lngMyRow).EntireRow.Hidden = False
                    End If
                Next lngMyRow
            Case Is = "Sheet6", "Sheet7", "Sheet8"
                For lngMyRow = 65 To 16 Step -1 'Need to work backwards through the rows when hiding or deleting
                    If Len(wsMySheet.Range("A" & lngMyRow)) = 0 Then
                        wsMySheet.Range("A" & lngMyRow).EntireRow.Hidden = False
                    End If
                Next lngMyRow
        End Select
    Next wsMySheet
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Actually if you want to just unhide every row in the each range for the applicable tab, you can do this:

Code:
Option Explicit
Sub Macro2()

    Dim wsMySheet As Worksheet
    Dim lngMyRow  As Long
    
    Application.ScreenUpdating = False
    
    For Each wsMySheet In ThisWorkbook.Sheets
        Select Case wsMySheet.Name
            Case Is = "Sheet2", "Sheet3", "Sheet4", "Sheet5"
                wsMySheet.Range("A9:A58").EntireRow.Hidden = False
            Case Is = "Sheet6", "Sheet7", "Sheet8"
                wsMySheet.Range("A16:A65").EntireRow.Hidden = False
        End Select
    Next wsMySheet
    
    Application.ScreenUpdating = True

End Sub

This will be much faster as there's no looping (although your datasets are quiet small so you mightn't notice a difference).

Robert
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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