kanishkgarg
New Member
- Joined
- Sep 29, 2021
- Messages
- 21
- Office Version
- 365
- Platform
- 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:
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:
I tried the following code but it wasnt what i wanted exactly as it simple calculated values for same values consecutive values:
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:
Date | ID | Currency | Quantity | Fees |
11.10 | 2561 | EUR | 10 | 55 |
11.10 | 2562 | EUR | 10 | 54 |
12.10 | 2561 | EUR | 10 | 53 |
13.10 | 2561 | EUR | 10 | 65 |
14.10 | 2561 | EUR | 10 | 56 |
14.10 | 2562 | EUR | 10 | 98 |
14.10 | 2563 | EUR | 10 | 26 |
15.10 | 2561 | EUR | 10 | 46 |
15.10 | 2563 | EUR | 10 | 79 |
16.10 | 2562 | EUR | 10 | 49 |
17.10 | 2562 | EUR | 10 | 33 |
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:
ID | Fees |
2561 | Sum 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