I need a ‘looping’ macro (relative) that sums cell values based on adjacent cell contents.
With the cursor on cell B4, for instance, start the macro.
It sets the contents of C4 as the compare-with variable, and sums the values of D4 through Dr, where r is the last row where Cr contents equal the compare-with variable, and the sum appears on B4.
At that point, it looks at Cr+1, and if not empty, replaces the ‘compare-with’ variable its contents. It now sums the values of Dr+1y through Dr+1y, where y is the last row where the contents of Cr+1y are the same as the newer compare-with variable, and the sum appears on Br+1.
Repeats until there are no values.
BTW, the list is sorted by the values in column C, so the compare-with values are grouped.
Please let me know if I can add anything to my explanation.
Thx!
Nando
Last edited by Nando01; 07-25-2011 at 02:05 PM. Reason: add one line
your explanation looks quite good but it should be better if you will add any workbook with dummy data.
Best Regards
I wasn't sure how to attach a file. I'll try - - -
Looks like it worked.
In this example, I manually inserted the SUM functions where I would like the macro to do it.
Thx!
Nando.
I tape macro for automate insert SUM formula
macro in sheet1 code, probably there is simplier way but I create following:
Best RegardsSub fillFormula() Dim last, beginR, endR As Long last = Cells.SpecialCells(11).Row beginR = 4 FormulaDestinationColumn = 2 For r = beginR + 1 To last + 1 If Cells(r - 1, 3).Value = Cells(r, 3).Value Then endR = r Else Cells(beginR, FormulaDestinationColumn).FormulaR1C1 = "=SUM(RC4:R[" & endR - beginR & "]C4)" beginR = r endR = r End If Next r End Sub
MaczaQ
Last edited by maczaq; 07-22-2011 at 09:40 AM. Reason: I found error, now should be OK.
maczaq -
That worked just fine - what I needed!
Thx!!!!
Alex
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks