AutoHide a Row If All Cells in Range are blank

CivilENGR

New Member
Joined
Jul 10, 2018
Messages
3
Trying to figure out how to write code such that If ALL the cells in a range, say (B4:Z4), are blank then hide the row. I need this to be applied to multiple ranges however, not just that specific range.

Idea is something like:

Sub HideRows()
For k = 4 To 40
IF All cells in Range ("B(k):Z(k)") = ""
Then
EntireRow.Hidden = True
Else
EntireRow.Hidden = False
Next k
End Sub

Fairly new to coding so not sure if I'm close or not... Any help is greatly appreciated!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Welcome to the Board!

Try this:
Code:
Sub HideRows()

    Dim k As Long
    Dim rng As Range
    
    Application.ScreenUpdating = False
    
    For k = 4 To 40
        Set rng = Range("B" & k & ":Z" & k)
        If Application.WorksheetFunction.CountBlank(rng) = 25 Then
            Rows(k).Hidden = True
        Else
            Rows(k).Hidden = False
        End If
    Next k
    
    Application.ScreenUpdating = True
        
End Sub
 
Upvote 0
Thanks! I think I'm almost there. I forgot to mention I'm using a command button to initiate this code (not sure if it makes a difference). I input the code but nothing happens when I click the command button (no errors either).

My code is:
Code:
Private Sub HideRows_Click()
    Dim i As Long
    Dim rng As Range
    
    Application.ScreenUpdating = False
    For i = 4 To 40
     Set rng = Range("B" & k & ":Z" & k)
        If Application.WorksheetFunction.CountBlank(rng) = 25 Then
        Rows(i).Hidden = True
      Else
        Rows(i).Hidden = False
     End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,746
Messages
6,132,475
Members
449,729
Latest member
davelevnt

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