variable start and finish for SUM

Gregor

New Member
Joined
Aug 20, 2002
Messages
12
Hi,
i want to find the sum for sections within a column, the start and finish of these sections is variable.
At the moment i have it calculating the sum for the first of these sections, the start of which i know, but after this point it doesn't have the start of the next sections and keeps returning the value for the first section.

This is what i have:

Sub colour()

Do Until IsEmpty(ActiveCell) And ActiveCell.Interior.ColorIndex <> 40
If ActiveCell.Interior.ColorIndex = 40 Then

ActiveCell.Offset(1, 0).Select
End If
Do Until ActiveCell.Interior.ColorIndex = 40

Application.WorksheetFunction.Sum(range("c13", range("c13").End(xlDown)))
ActiveCell.Offset(1, 0).Select

Loop

MsgBox "...and the sum is " & summy

Loop
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Jim North

MrExcel MVP
Joined
Jun 20, 2002
Messages
791
Hello Gregor,

To be honest, I can't quite determine what you are trying to do... but I made some changes to your code that SHOULD point you in the right direction. THIS IS NOT TESTED and NEEDS major changes to work properly.

<pre>
Sub colour()

Dim StartRow As Long

StartRow = 13

Do Until IsEmpty(ActiveCell) And ActiveCell.Interior.ColorIndex <> 40

If ActiveCell.Interior.ColorIndex = 40 Then
ActiveCell.Offset(1, 0).Select
End If

Do Until ActiveCell.Interior.ColorIndex = 40
Application.WorksheetFunction.Sum ("C" & StartRow & ":C" & ActiveCell.Row)
StartRow = ActiveCell.Row
ActiveCell.Offset(1, 0).Select
Loop

MsgBox "...and the sum is "

Loop

End Sub
</pre>
 

Gregor

New Member
Joined
Aug 20, 2002
Messages
12
Hi,
thanks for your help...I've been playing around with it and have come up with this but i still have some problems (see below):

Sub colour2()

StartRow = c13

Do Until ActiveCell = range("A65536").End(xlUp)

If ActiveCell.Interior.ColorIndex = 40 Then
ActiveCell.Offset(1, 0).Select
End If

If ActiveCell = "" Then
Exit Sub
End If

' Do Until ActiveCell.Interior.ColorIndex = 40
' summy = Application.WorksheetFunction.Sum(range("c13", range("c13").End(xlDown)))


Do Until ActiveCell.Interior.ColorIndex = 40
summy = Application.WorksheetFunction.Sum(range(StartRow & range(StartRow).End(xlDown)))
StartRow = ActiveCell.Row

If ActiveCell <> "" Then
ActiveCell.Offset(1, 0).Select
End If

Loop

ActiveCell = summy

MsgBox "...and the sum is " & summy
Loop

End Sub

The part that is messaged out at the moment does what I want to a point....it inserts sub-totals when it reaches cells of a certain colour and stops when it reaches a blank cell that does not contain this colour, however the figures that are inserted are wholly cumulative. What i want them to do it reset after each sub-total is put in. The code that is being used at the moment only provides the sum for the last two figures before the cell the sub-total goes into (???).
I'm possibly completely off the mark with the current stuff but I'm really at a standstill, any ideas?
 

Gregor

New Member
Joined
Aug 20, 2002
Messages
12

ADVERTISEMENT

Hi,
sorry this seems to be taking so long, but the problem seems centred more on the line:

summy = Application.WorksheetFunction.Sum(range("c13", range("c13").End(xlDown)))

This incurrs a running total from cell 13, if i reset to zero it doesn't matter because once i'm in the loop again it sums from c13.

i have tried:

summy = Application.WorksheetFunction.Sum(range(activecell, activecell.End(xlDown)))

but this sums only the cells directly above and below the cells with colorindex of 40...

I've tried just about eveything i can think of....I'd really appreciate any more suggestions...
 

Jim North

MrExcel MVP
Joined
Jun 20, 2002
Messages
791
Gregor,

Could you please post a sample of your spreadsheet to the board. If you haven't done this before, you use the utility you will find at the bottom of this web page referred to as "Colo's Cool Utility". It will help you create HTML code that you can copy into your message for us to see.
 

Gregor

New Member
Joined
Aug 20, 2002
Messages
12
well this message was to include the image but when I click on the button that is supposed to send it to my clipboard nothing happens....this just seems to be one thing after another...

what to do...
 

Forum statistics

Threads
1,144,117
Messages
5,722,576
Members
422,446
Latest member
srclife

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
Top