Macro that hides row if all rows within a given range are empty

Anonymous321

New Member
Joined
Oct 12, 2021
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm wondering if it's possible to set up a macro that hides a certain row if all rows within a given range are hidden.
For example, if rows 5 to 9 are hidden then hide row 4. I made a code that works on a single row but I want it to work for multiple rows. My problem at the moment is that if row 5 is empty, for example, row 4 will be hidden regardless to whether or not the other rows in the range are not hidden,

This is the code I'm currently using.

If Range("6:10").EntireRow.Hidden Then
Range("5:5").EntireRow.Hidden = True
End If


Any help will be appreciated
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this, seems stupid but it was working for me.

Excel Formula:
If Range("6:6").EntireRow.Hidden And Range("7:10").EntireRow.Hidden Then
Range("5:5").EntireRow.Hidden = True
End If
 
Upvote 0
Try this, seems stupid but it was working for me.

Excel Formula:
If Range("6:6").EntireRow.Hidden And Range("7:10").EntireRow.Hidden Then
Range("5:5").EntireRow.Hidden = True
End If
Still running into the same problem sadly :(. Row 8 Would not be hidden yet it still hides row 5.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Macro that hides row if all rows within a given range are hidden
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Macro that hides row if all rows within a given range are hidden
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then the
Sorry, didn't realise there were rules against it
 
Upvote 0
Will A5:A10 always contain values?
 
Upvote 0
In that case how about
VBA Code:
   If Application.Subtotal(103, Range("A6:A10")) = 0 Then
      Rows(5).Hidden = True
   End If
if rows 6 to 10 are all hidden row 5 will hide.
 
Upvote 0
Solution
In that case how about
VBA Code:
   If Application.Subtotal(103, Range("A6:A10")) = 0 Then
      Rows(5).Hidden = True
   End If
if rows 6 to 10 are all hidden row 5 will hide.
Yes! Thank you, that seems to work. Can I ask quickly what the function of the '103' in in this code, just so I know in the future when a replicate this code? Thanks again, I've been trying to get this to work all day :)
 
Upvote 0
It tells the subtotal function to count any values ignoring cells that are hidden.
 
Upvote 0

Forum statistics

Threads
1,215,331
Messages
6,124,311
Members
449,152
Latest member
PressEscape

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