How to Automatically Hide/Unhide Rows Based on Cell Value

jamber17

New Member
Joined
Jan 22, 2018
Messages
6
I have a worksheet where column A data changes when you select a different drop down option (ex - hours, weekdays, months, etc.). But I don't want the blank rows to show. For example, there are currently 53 rows for the 53 weeks in a year, but when I switch the data to Weekdays I want it to only show the 7 rows for the 7 weekdays. I don't want this to be a filter but a recurring VBA code that automatically updates when the data type changes (cell B5). I've attached a picture and the current code that I'm using, but the code isn't changing automatically.

Sub HURows()
BeginRow = 9
EndRow = 60
ChkCol = 1

For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 0 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
End Sub



1602610371739.png
 

Attachments

  • 1602610303475.png
    1602610303475.png
    13.8 KB · Views: 77
  • 1602610323616.png
    1602610323616.png
    19.2 KB · Views: 77

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim RowCnt As Long
   
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "B5" Then
   For RowCnt = 9 To 60
      Rows(RowCnt).Hidden = Cells(RowCnt, 1).Value = 0
   Next RowCnt
End Sub
This needs to go in the relevant sheet module.
 
Upvote 0
Oops missed the end if, it should be
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim RowCnt As Long
   
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "B5" Then
      For RowCnt = 9 To 60
         Rows(RowCnt).Hidden = Cells(RowCnt, 1).Value = 0
      Next RowCnt
   End If
End Sub
 
Upvote 0
For some reason it's not working. I'm super new to VBA, so can you tell me if I'm doing something wrong? Here's the error I keep getting.

1602615303599.png
 
Upvote 0
You need to delete that code from the module & put it into the Sales & Penetration Dashboard module.
Then when you manually change B5 the rows will (un)hide
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,947
Members
449,095
Latest member
nmaske

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