I'm trying to create a simple macro, and the solution is eluding me.
Let's say I have a table:
| Header1 | Header2 |
|---|---|
| AAA | 100 |
| BBB | 200 |
| BBB | 150 |
| BBB | 250 |
| CCC | 300 |
| CCC | 200 |
I want the formula to calculate so that totals will calculate Everything based on variables in A1 separately. And I don't want it to be limited to just 3 options, there are indefinite amount of variables in A1. On top of that, how many of each variable will be different. So I cannot use basic functions like B3:B5, because BBB could have 5 lines one day, but 8 lines the next.
But for the sake of this example, I want it to show the totals for each different variable. IE: AAA = 100, BBB = 600, CCC = 500.
Currently I have another function that numbers each line based on the variable, so all AAA would be 1, all BBB is 2, and all CCC is 3, by using:
=IF(A3\<\>A2,G2+1,G2)
I tried using similar applications of this for the value calculation part, but nothing seems to work.
If you want to have a summary of all the data, you can use the GROUPBY function in the new Excel:
=GROUPBY($A:.$A,$B:.$B,SUM,3,0)
I assume that columns A and B contain only these data.
(https://i.sstatic.net/M2glwgpB.png)
In earlier versions of Excel, you can use a Consolidate procedure or an equivalent macro:
Sub Consolidate()
Dim src As Range
Set src = Range("A1").CurrentRegion
Range("D1").Consolidate Sources:=src.Address(ReferenceStyle:=xlR1C1, External:=True), _
Function:=xlSum, TopRow:=True, LeftColumn:=True
Range("D1").Value = Range("A1").Value
End Sub