Sum values based on unique ID

briguyUSA

New Member
Joined
Jun 10, 2021
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
I have written some VBA code that allows me to filter a spreadsheet to the data I require . I am now looking to add all of the values in a column that have the same unique ID. For example, if there are 4 rows with the same ID of "123456", I want to add up all of their corresponding values to a "sum" row. Any tips on how i might tackle this?

I also won't be able to simply apply a SumIF funciton, as the filter updates with either more or less rows and such.
ID​
Values​
123456​
10​
123456​
12​
123456​
14​
123456​
16​
Sum​
52​
987654​
20​
987654​
22​
Sum​
42​
456789​
30​
456789​
50​
456789​
10​
Sum​
90​
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try:
VBA Code:
Sub SumValues()
    Application.ScreenUpdating = False
    Dim ID As Range, x As Long, i As Long: i = 1
    For Each ID In Range("A2", Range("A" & Rows.Count).End(xlUp))
        If ID <> "Sum" And ID.Offset(1) <> ID Then
            Rows(ID.Row + 1).Insert
            Range("A" & (ID.Row + 1)) = "Sum"
            x = Range("B2", Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas.Item(i).Cells.Count - 1
            Range("B" & ID.Row + 1).Formula = "=Sum(B" & ID.Row - x & ":B" & ID.Row & ")"
            i = i + 1
        End If
    Next ID
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub SumValues()
    Application.ScreenUpdating = False
    Dim ID As Range, x As Long, i As Long: i = 1
    For Each ID In Range("A2", Range("A" & Rows.Count).End(xlUp))
        If ID <> "Sum" And ID.Offset(1) <> ID Then
            Rows(ID.Row + 1).Insert
            Range("A" & (ID.Row + 1)) = "Sum"
            x = Range("B2", Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas.Item(i).Cells.Count - 1
            Range("B" & ID.Row + 1).Formula = "=Sum(B" & ID.Row - x & ":B" & ID.Row & ")"
            i = i + 1
        End If
    Next ID
    Application.ScreenUpdating = True
End Sub
Thank you so much for your assistance!!!!
 
Upvote 0
@mumps I search for macro like this but my problem is the duplicated items are not arranged consecutively

the original data in col A,B and the result in col G,H with put the formatting and borders and colors and put the data in cell center .
note: the code should create whole data with headers in COL G,H

can you mod the code , please ?
sum dupد.xlsm
ABCDEFGH
1idvaidva
2AA-10010AA-10010
3AA-1005AA-1005
4AA-10110AA-10020
5AA-10020SUM35
6AA-1015AA-10110
7AA-10210AA-1015
8AA-10320AA-1015
9AA-10220SUM20
10AA-10310AA-10210
11AA-1015AA-10220
12AA-1045SUM30
13AA-1045
14SUM5
15
16
17
sheet1

thanks in advance
 
Upvote 0
This macro will place the result in Sheet2.
VBA Code:
Sub SumValues()
    Application.ScreenUpdating = False
    Dim lRow As Long, i As Long, v As Variant, va As Range, total As Long, desWS As Worksheet
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Set desWS = Sheets("Sheet2")
    v = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
    With desWS.Range("A1:B1")
        .Value = Array("id", "va")
        .Font.Bold = True
    End With
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v, 1)
            If Not .Exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                With Range("A1")
                    .CurrentRegion.AutoFilter 1, v(i, 1)
                    For Each va In Range("B2:B" & lRow).SpecialCells(xlCellTypeVisible)
                        total = total + va.Value
                    Next va
                    With desWS
                        Range("A2:B" & lRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
                        .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Interior.ColorIndex = 6
                        .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Interior.ColorIndex = 3
                        With .Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 2)
                            .Value = Array("Sum", total)
                            .Font.Bold = True
                            .Borders.LineStyle = xlContinuous
                        End With
                    End With
                End With
                total = 0
            End If
        Next i
        Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
@mumps it gives error 'object variable or with block variable not set " in this line
lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
Upvote 0
Make sure that the sheet containing the data is the active sheet.
 
Upvote 0
I tested the macro in a dummy file using the data you posted and it worked properly. Could upload a copy of your file (de-sensitized if necessary) to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here.
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,011
Members
448,935
Latest member
ijat

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