Is it possible to automatically hide/unhide rows based on the contents of a cell in that row?

slam

Well-known Member
Joined
Sep 16, 2002
Messages
921
Office Version
  1. 365
  2. 2019
I have a chart based on A1:T36

Sometimes there will a #N/A in in column A based on the results of the formula contained within.

I want those rows automatically hidden, but should the result change to anything else, I'd want it unhidden.

Is this possible? Maybe some VB that runs upon worksheet activate?

Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Sure, try this

Right click the Sheet's Tab - View Code
paste the following

Rich (BB code):
Private Sub Worksheet_Calculate()
Dim MyRange As Range, C As Range 
Application.EnableEvents = False
Set MyRange = Range("A1:A36") 'Adjust range as needed.
 
For Each C In MyRange
    C.EntireRow.Hidden = IsError(C)
Next C
 
Application.EnableEvents = True
End Sub

Hope that helps.
 
Upvote 0
Sure, try this

Right click the Sheet's Tab - View Code
paste the following

Rich (BB code):
Private Sub Worksheet_Calculate()
Dim MyRange As Range, C As Range 
Application.EnableEvents = False
Set MyRange = Range("A1:A36") 'Adjust range as needed.
 
For Each C In MyRange
    C.EntireRow.Hidden = IsError(C)
Next C
 
Application.EnableEvents = True
End Sub

Hope that helps.

I'm getting a Compile error: Object Required on My Range = and the first row is also highlighted yellow - nothing being hidden so far.

Does this run each time the workbook is opened? Can it be changed to run each time the worksheet is activated?

Incidentally, don't know if it helps, but the formula in A2 through A36 is:

=IF('Drivers & Standings'!B3="",NA(),'Drivers & Standings'!B3)
 
Upvote 0
I'm getting a Compile error: Object Required on My Range = and the first row is also highlighted yellow - nothing being hidden so far.

Does this run each time the workbook is opened? Can it be changed to run each time the worksheet is activated?

Incidentally, don't know if it helps, but the formula in A2 through A36 is:

=IF('Drivers & Standings'!B3="",NA(),'Drivers & Standings'!B3)

I did a typo dimming the variables as Long instead of Range.
I've already corrected it, recopy the code from the previous post.


It runs every time a formula recalculates.
 
Upvote 0
Glad to help, thanks for the feedback...


But Andrew's suggestion to use autofilter will be more efficient..
With a dataset of only 36 rows, it's not that big of a deal.
But if you're going to expand it in the future, you might try this.
Assuming Row 1 is headers..

Code:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
Range("A1:T36").AutoFilter Field:=1, Criteria1:="<>#N/A"
Application.EnableEvents = True
End Sub
 
Upvote 0
I don't really like the look of autofilter - the arrows are messy and obscure the header row text :)
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

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