VBA Macro to group rows that are colored

shyy

Well-known Member
Joined
Nov 6, 2008
Messages
1,484
Hey guys,

I am in need of some assistance with VBA. I have in column A certain rows that have color. Is it possible to have a macro that will automatically group rows between the colors? In this example range A1:A19 would be grouped together. I have formula to the right of the colored cell to represent the partner's name. This way I can collapse/expand data easily. I have about 16,000 rows of data split between 20 partners. You can see why I want to collapse/expand information for certain partners.

Thanks in advance.

Excel 2010
A
1651113
2Final K-1 Amended K-1 OMB No. 1545-0099 Schedule K-1
3(Form 1065) Partner's Share of Current Year Income,
4Deductions, Credits, and Other Items
5A.ƒÊ�E Part III
6Department of the Treasury For calendar year 2013, or tax
7Internal Revenue Service
81
92
103
114
125
136a
146b
157
168
179a
189b
199c
20Ordinary business income (loss)

<colgroup><col style="width: 25pxpx"><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello,
Try this with an ActiveX button or in a standard Module:

Dim rng As Range
Range("A1:A19").Rows.Ungroup
For Each rng In Range("A1:A20").Cells
If rng.Interior.ColorIndex = xlNone Then
rng.Rows.Group
End If
Next

The code takes care of the changes that may occur.
 
Upvote 0
Sorry I've to correct myself thou,
This code works better:

Code:
Dim rng As Range
On Error Resume Next
For Each rng In Range("A1:A20").Cells
If rng.Interior.ColorIndex = xlNone Then
rng.Rows.Ungroup
rng.Rows.Group
End If
Next
 
Upvote 0
Or this:

Code:
Dim rng As Range
On Error Resume Next
For Each rng In Range("A1:A20").Cells
rng.Rows.Ungroup
If rng.Interior.ColorIndex = xlNone Then
rng.Rows.Group
End If
Next
 
Upvote 0
Or this:

Code:
Dim rng As Range
On Error Resume Next
For Each rng In Range("A1:A20").Cells
rng.Rows.Ungroup
If rng.Interior.ColorIndex = xlNone Then
rng.Rows.Group
End If
Next

How could these two lines be used to get the macro to run on worksheets with different number of total rows?
Code:
Dim rowCount As Integer
rowCount = Cells(Rows.Count, myRange.Column).End(xlUp).Row

Thanks by the way for your original macro. Worked like a charm
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,753
Messages
6,126,677
Members
449,327
Latest member
John4520

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