Macro to hide rows based on cell content

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
370
I need to hide rows based on a cell value if blank or has a "-". I grabbed the code below online but I assume it can be improved. I do want the sheet name specified because I run this for several sheets under a global macro as well. Any help is appreciated. Thanks

Sub CLUTCH_BUILD_HIDEROWS()
Sheets("CLUTCH BUILD").Activate
Dim xRg As Range
Application.ScreenUpdating = False
For Each xRg In Range("B9:B18")
If xRg.Value = "" Or xRg.Value = "-" Then
xRg.EntireRow.Hidden = True

Else
xRg.EntireRow.Hidden = False
End If
Next xRg
Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try:
Code:
Sub CLUTCH_BUILD_HIDEROWS()
    Dim x As Long
    Application.ScreenUpdating = False
    For x = 10 To 2 Step -1
    If Sheets("CLUTCH BUILD").Cells(x, 2) = "" Or Sheets("CLUTCH BUILD").Cells(x, 2) = "-" Then
            Sheets("CLUTCH BUILD").Rows(x).EntireRow.Hidden = True
        Else
            Sheets("CLUTCH BUILD").Rows(x).EntireRow.Hidden = False
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Try:
Code:
Sub CLUTCH_BUILD_HIDEROWS()
 Dim xRg As Long
  Application.ScreenUpdating = False
   With Sheets("CLUTCH BUILD")
    .Rows("9:18").Hidden = False
     For xRg = 9 To 18
      .Rows(xRg).Hidden = .Cells(xRg, 2) = "" Or .Cells(xRg, 2) = "-"
     Next xRg
   End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
I don't see a range of cells specified. It should only hide rows based on the criteria in cell range B9 to B18.
 
Upvote 0
The macros are based on row numbers rather than ranges. I made a slight error in my previous macro.
Code:
Sub CLUTCH_BUILD_HIDEROWS()
    Dim x As Long
    Application.ScreenUpdating = False
    For x = 18 To 9 Step -1
    If Sheets("CLUTCH BUILD").Cells(x, 2) = "" Or Sheets("CLUTCH BUILD").Cells(x, 2) = "-" Then
            Sheets("CLUTCH BUILD").Rows(x).EntireRow.Hidden = True
        Else
            Sheets("CLUTCH BUILD").Rows(x).EntireRow.Hidden = False
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Thanks it works fine now. So I see that the x refers to the row and the 2 is the column?

What would the code be to unhide those columns in a separate macro?
 
Upvote 0
Try:
Code:
Sub UnhideRows()
    Sheets("CLUTCH BUILD").Cells.EntireRow.Hidden = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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