How to calculate sum for unique values in a different column

kanishkgarg

New Member
Joined
Sep 29, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi,

I need to make a macro which calculates the sum for each unique ID and paste it in a new workbook. My data looks something like this:

DateIDCurrencyQuantityFees
11.102561EUR1055
11.102562EUR1054
12.102561EUR1053
13.102561EUR1065
14.102561EUR1056
14.102562EUR1098
14.102563EUR1026
15.102561EUR1046
15.102563EUR1079
16.102562EUR1049
17.102562EUR1033


so for example, it should calculate all the fees for customer ID 2561 for all the days there is data and similarly, for the the other customers too and paste it as follows in the new workbook:

IDFees
2561Sum of all fees which have 2561 in column B
2562"
2563"
2564"

I tried the following code but it wasnt what i wanted exactly as it simple calculated values for same values consecutive values:

VBA Code:
Sub SumValues()
    Application.ScreenUpdating = False
    Dim ID As Range, x As Long, i As Long: i = 1
    For Each ID In Range("B2", Range("B" & Rows.Count).End(xlUp))
        If ID <> "Sum" And ID.Offset(1) <> ID Then
            Rows(ID.Row + 1).Insert
            Range("B" & (ID.Row + 1)) = "Sum"
            x = Range("K2", Range("K" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas.Item(i).Cells.Count - 1
            Range("K" & ID.Row + 1).Formula = "=Sum(K" & ID.Row - x & ":K" & ID.Row & ")"
            i = i + 1
        End If
    Next ID
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Do you actually mean a workbook or is it a worksheet?
I see you have office 365, it's easy with dynamic array formula's. Using a macro for this could be more complicated

Anyway, here is an example macro, writing the output to the same sheet.

VBA Code:
Sub jec()
 ar = Cells(1, 1).CurrentRegion
  With CreateObject("scripting.dictionary")
    For i = 2 To UBound(ar)
      .Item(ar(i, 2)) = .Item(ar(i, 2)) + ar(i, 5)
    Next
  Cells(1, 10).Resize(.Count, 2) = Application.Transpose(Array(.keys, .items))
 End With
End Sub
 
Upvote 0
Do you actually mean a workbook or is it a worksheet?
I see you have office 365, it's easy with dynamic array formula's. Using a macro for this could be more complicated
Yes I would like to paste this in a different workbook. I do know I haven't done this in my macro. I will need a macro due to some work requirements.
 
Upvote 0
Do you actually mean a workbook or is it a worksheet?
I see you have office 365, it's easy with dynamic array formula's. Using a macro for this could be more complicated

Anyway, here is an example macro, writing the output to the same sheet.

VBA Code:
Sub jec()
 ar = Cells(1, 1).CurrentRegion
  With CreateObject("scripting.dictionary")
    For i = 2 To UBound(ar)
      .Item(ar(i, 2)) = .Item(ar(i, 2)) + ar(i, 5)
    Next
  Cells(1, 10).Resize(.Count, 2) = Application.Transpose(Array(.keys, .items))
 End With
End Sub
Hi, thank you so much for the solution. However, i need to change some details on this. The fees column in my dataset is actually in column J. When i try to change ar(i,5) to ar(i,11) it doesnt work. Also, I would only like to paste data which have value more than zero. Is that possible?

Thanks in advance!
 
Upvote 0
Hi, thank you so much for the solution. However, i need to change some details on this. The fees column in my dataset is actually in column J. When i try to change ar(i,5) to ar(i,11) it doesnt work. Also, I would only like to paste data which have value more than zero. Is that possible?

Thanks in advance!
I should mention, it is working for all columns except the one i am trying as this has some # N/As which i dont want to remove as those have some significance in my analysis.
 
Upvote 0

Forum statistics

Threads
1,214,846
Messages
6,121,905
Members
449,054
Latest member
luca142

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