PDA

View Full Version : VBA help - how to ungroup rows at level 2 only



Buddha_420
10-26-2015, 12:53 AM
Hello guys,

I'm running a macro to conditionally group rows based on cell values (if cell in column B = "n/a"). The thing is that I want some rows to remain grouped at all time and have some of these already grouped rows to be grouped to level 2 if the aforementioned condition is met.

The issue with the code below is that if I run the macro more than once, some rows will be grouped more than twice (which is what I want to avoid). I need to have level 2 grouped rows to be ungrouped before checking for the condition and grouping but can't find the code for that.


Thanks for helping!




OptionExplicit
Sub GroupRows()
Dim rData, rCel As Range
Set rData = Range("b47", Range("b" & Rows.Count).End(xlUp))

Application.ScreenUpdating = False
With rData
OnErrorResumeNext
.Rows.EntireRow.Hidden = False
OnErrorGoTo 0
EndWith
ForEach rCel In rData
If rCel = "END"ThenExitFor
If rCel = "n/a"Then
Rows(rCel.Row).Group
rCel.EntireRow.Hidden = True
EndIf
Next
Application.ScreenUpdating = True


EndSub

Buddha_420
10-26-2015, 10:40 AM
No one?!

I'm basically trying to get the code that says
"if row grouped at level 2 --> ungroup"