Loop to Ignore Hidden Rows

JessP

New Member
Joined
Jan 11, 2018
Messages
23
Hello,

I have a worksheet that's split into groups of data with a heading at the top of each. For instance, row 3 has a heading, then 4-9 have data that goes with it, then row 10 has another heading, and so on. Some of the rows of data are hidden using VBA based on certain values, leaving the occasional header with no data under it.

I'd like to hide the data-less headers, but because there are hidden rows in between, I'm struggling to catch the right rows. Basically, every header row is blank in col M and very row with data is not. Here's the code I've been playing with, but it doesn't continue until it's found the next visible row. Any thoughts on how to make it work?

Code:
Dim N as long
Dim I as long
Dim x as long

N = Sheets("FoE").Range(Cells(Rows.Count, "B")).End(xlUp)
For i = 4 To N
x = 1
If Cells(i, 13).Value = "" Then
    If Cells(i, 13).Offset(x, 0).Hidden = True Then
        x = x + 1
    ElseIf Cells(i, 13).Offset(x, 0).Value = "" Then
        Range(i, 13).EntireRow.Hidden = True
    End If
End If
Next i
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I think this snippet of code will do what you intended the snippet of code you posted to do...
Code:
[table="width: 500"]
[tr]
	[td]  Dim Cell As Range, LastVisibleCell As Variant
  LastVisibleCell = "M3"
  For Each Cell In Range("M4:M" & Cells(Rows.Count, "M").End(xlUp).Row + 1).SpecialCells(xlVisible)
    If Cell.Value = "" And Range(LastVisibleCell) = "" Then Range(LastVisibleCell).EntireRow.Hidden = True
    LastVisibleCell = Cell.Address
  Next[/td]
[/tr]
[/table]
 
Upvote 0
Thanks, Rick, that does exactly what I was looking for - and much more elegantly than my attempt.
 
Upvote 0
Thanks, Rick, that does exactly what I was looking for - and much more elegantly than my attempt.
You are welcome, but there is a minor flaw in my code. Because the SpecialCells method only looks at the UsedRange, my previous code cannot hide the last header if its data is all hidden. This following code snippet fixes the problem...
Code:
[table="width: 500"]
[tr]
	[td]  Dim BottomRow As Long, LastVisibleCell As Variant, Ar As Variant, Cell As Range
  Set Ar = Rows.SpecialCells(xlVisible).Areas
  BottomRow = Ar(Ar.Count).Row
  Cells(BottomRow, "A") = ""
  LastVisibleCell = "M3"
  For Each Cell In Range("M4:M" & BottomRow).SpecialCells(xlVisible)
    If Cell.Value = "" And Range(LastVisibleCell) = "" Then Range(LastVisibleCell).EntireRow.Hidden = True
    LastVisibleCell = Cell.Address
  Next
  Cells(BottomRow, "A").Clear
[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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