I have the code below that doesn’t seem to be working. Essentially, rngList refers to a defined name range in Excel that is about 500 rows long and every n number of rows there is text (there are approximately 32 rows out of the 500 that have text). I am trying to go to the non-blank cells (by mimicking the ctrl + down command in Excel).
I am checking to see if they are blank, and if they are I want to group that cell. If it is not blank, I want to check the cell to the left and if it is 0, I also want to group it. The code I have now is essentially trying to do this but I am receiving the error below:
Group Method of Range Class Failed
It then goes on to highlight the following line:
Selection.Rows.Group
EDIT: Let’s say instead of grouping rows that are blank, I want to group rows that have 1 in them. That way the crtl + down will actually go to that cell rather than the last row.
Thank you very much for the help!
The code is below:
rngList.Cells(1).Select
i = 0
Do While i < 32
i = i + 1
If Selection.Value = "" Then
Selection.Rows.Group
Else
Selection.End(xlToLeft).Select
If Selection.Value <> 0 Then
Selection.Rows.ClearOutline
End If
End If
Selection.End(xlToRight).Select
Selection.End(xlDown).Select
Loop
Despite the age of this post, I thought I’d throw in my two cents for anyone who might stumble upon it. I hope I understand your question correctly. Here’s what I’ve gathered:
Goal: For every row in the column of interest, group rows based on a criteria.
Criteria: The only
rows in the groupare those that either have no value (blank, null, empty) OR have a value AND have a neighboring cell (directly to the left) that has a value of 0. The onlyrows not in the groupare those that are not blank and have a neighboring cell that is not 0.Here is some sample data:
Data Before Running Macro:
Data After Running Macro – Grouping Uncollapsed:
Data After Running Macro – Grouping Collapsed:
The code that handles this:
To make this code work: In the VBE (Visual Basic Editor), open the worksheet that contains the data to group (also contains the named range
rngList) and paste this code, then run the macro.Note: The comments are added to explain certain parts in further detail, though I believe the code itself is written in a way that can explain itself (e.g. variable names are meaningful and logic makes sense).