Unhide or hide rows based on cell values in each row

jkehoe

New Member
Joined
Feb 8, 2009
Messages
7
Hi all - my first post here.

I have a worksheet with a series of sequential numbers in Column F, typically running from about 25 or 30 in Row 6, and increasing by 1 in each row below that, to a maximum number in Row 85. I'd like to hide the rows in which the number in Column F is greater than 90. The procedure needs to run whenever the sheet is calculated. The values in the column can change based on either of 2 inputs on another sheet, so I also need the procedure to unhide rows in which the number is 90 or less. The code I have so far is:

VBA Code:
Private Sub Worksheet_Calculate()
    Dim Cl As Range
    For Each Cl In Range("F6:F85")
        Cl.EntireRow.Hidden = False
        Cl.EntireRow.Hidden = Cl.Value > 90
    Next Cl
End Sub

... But it's not working. I get an "application-defined or object-defined" error. Can anyone see what I'm missing here?

BTW - I've also tried calling this procedure from Sheet1 (using the Worksheet_Change event), but I can't get it to run the code on Sheet2. To be clear - there are 2 inputs on Sheet1, and I want Sheet2's row-unhiding/hiding procedure to run if either input changes.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
There's nothing wrong with the code, but you don't need the first the first line in the loop, you can just use
VBA Code:
Private Sub Worksheet_Calculate()
    Dim Cl As Range
    For Each Cl In Range("F6:F85")
        Cl.EntireRow.Hidden = Cl.Value > 90
    Next Cl
End Sub
which line is highlighted when you get the error?
 
Upvote 0
It's the line you've identified which has been highlighted before, but actually right now when I ran it to check on this, it didn't highlight and I can no longer select any cells anywhere.

Without the first line in the loop, the rows only hide, and if the values subsequently change to =<90, the rows won't "unhide", will they?
 
Upvote 0
OK I removed the line but now when I try to run it or even save the workbook I get the following error: "Run-time error '1004': Unable to set the Hidden property of the Range class".

Thanks, BTW, for helping with this!
 
Upvote 0
Is your sheet protected & do you have any merged cells?
 
Upvote 0
Not protected, and no merged cells on Sheet2. When I mouse over the line in question, there's a callout which says "Unable to get the hidden property of the range class".
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Calculate()
    Dim Cl As Range
    Application.EnableEvents = False
    For Each Cl In Range("F6:F85")
        Cl.EntireRow.Hidden = Cl.Value > 90
    Next Cl
    Application.EnableEvents = True
End Sub
 
Upvote 0
OK that did it. Thanks so much! Do you know what the Application.EnableEvents switch would have stopped from causing the error?
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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