VBA: Sum based on Column Criteria

Tiger9136

New Member
Joined
Jul 10, 2017
Messages
19
Hello, I am needing help to sum values for each series. The series is in column A and the values are in column B. I have place an example at the bottom. I would like to do this with VBA.

Thanks


What I HaveWhat I Need
SERIALVALUESERIALVALUE
221122110
2212221
2213221
2214221
445544518
4456445
4457445
777877727
7779777
77710777

<colgroup><col span="2"><col><col span="2"></colgroup><tbody>
</tbody>
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try this for results starting "D2".
Code:
[COLOR="Navy"]Sub[/COLOR] MG07Dec24
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Temp [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]Set[/COLOR] Temp = Rng(1)
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not Dn.Value = Temp [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]Set[/COLOR] Temp = Dn
    [COLOR="Navy"]End[/COLOR] If
Dn.Offset(, 3) = Dn.Value
Temp.Offset(, 4) = Temp.Offset(, 4) + Dn.Offset(, 1).Value
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Assuming that your data is in columns A and B and you want to replace the current values in column B with your desired result, try this:
Code:
Sub MyMacro()
'
    Dim lastRow As Long
    
'   Find last row with data in column A
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

'   Populate column C with formula
    Range("C1").FormulaR1C1 = "VALUE"
    Range("C2:C" & lastRow).FormulaR1C1 = _
        "=IF(RC[-2]<>R[-1]C[-2],SUMIF(C[-2],RC[-2],C[-1]),"""")"

'   Copy values from column C to column B
    Columns("C:C").Copy
    Range("B1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    
'   Delete column C
    Columns("C:C").Delete Shift:=xlToLeft
    
End Sub
 
Upvote 0
This seems to do exactly what I need except it deletes the column C which contains descriptions, is there a way to do this without using column c?

Thanks
 
Upvote 0
I can use this code, I changed C to Q which is the next empty column. The issue that I am having is that the actual values are in column K instead of B. Can you show me where to adjust so it will use column A for the serial, K for the values and Q for the formula. Thanks
 
Upvote 0
See if this does what you want:
Code:
Sub MyMacro()
'
    Dim lastRow As Long
    
'   Find last row with data in column A
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

'   Populate column Q with formula
    Range("Q2:Q" & lastRow).FormulaR1C1 = _
        "=IF(RC[-16]<>R[-1]C[-16],SUMIF(C[-16],RC[-16],C[-6]),"""")"

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,147
Messages
6,123,297
Members
449,095
Latest member
Chestertim

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