VBA to Sum Range Between Colors

Jefe187

New Member
Joined
Oct 24, 2015
Messages
13
I have a worksheet that will be copied from a pivot table. The columns will remain static however the rows will change with each new pivot table. I have a code that will highlight the header columns blue. Is there a VBA code to sum the ranges between the 2 colors? I have provided an example below to help explain. I am unable to paste a screen shot or color the cells blue. Essentially in my spreadsheet the row that says "engines" and "Cabs" will be filled with blue and I need to sum each column between the blue fill.

[TABLE="width: 387"]
<tbody>[TR]
[TD]
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][TABLE="width: 500"]
<tbody>[TR]
[TD]Engines[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]255[/TD]
[TD]111[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]555[/TD]
[TD]656[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]353[/TD]
[TD]788[/TD]
[/TR]
[TR]
[TD]Cabs[/TD]
[TD]<blue>[/TD]
[TD]<blue>[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Simply enter =Sum_Colour() into one of the shaded cells

Code:
Function Sum_Colour()
Application.Volatile
Dim Colour As Long
Dim i As Long
Colour = Parent.Application.Caller.Interior.Color
For i = 1 To (Rows.Count - Parent.Application.Caller.Row)
    If Parent.Application.Caller.Offset(i).Interior.Color = Colour Then Exit Function
    Sum_Colour = Sum_Colour + Application.Caller.Offset(i)
Next i
End Function


Note: if you enter this into a cell that doesn't have any shaded cells below it, the function will take ages to calculate.
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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