Add all of Column A's, dependant on unique values in Column B

gray_b

New Member
Joined
Apr 17, 2019
Messages
40
Office Version
  1. 365
Platform
  1. Windows
I just cannot get my head around looping.
But I am trying to add all of Column A's, dependant on unique values in Column B
Any ideas please.

A B
Total Country
3 australia
3 australia
4 australia
2 Canada
2 Finland
2 Canada
3 Norway
3 ttxxyyzz
3 australia
4 Norway
4 australia
5 Finland
5 Italy
5 Italy
10 Finland

Result

A B
Grand Total Country
17 australia
4 Canada
17 Finland
7 Norway
3 ttxxyyzz
10 Italy

I can sort column B into unique values
Range("B2:B16").Select
Selection.Copy
Range("E2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$E$2:$E$16").RemoveDuplicates Columns:=1, Header:=xlNo
 
How about with a formula
Fluff.xlsm
ABCDE
1TotalCountryCountryTotal
23australiaaustralia17
33australiaCanada4
44australiaFinland17
52CanadaItaly10
62FinlandNorway7
72Canadattxxyyzz3
83Norway
93ttxxyyzz
103australia
114Norway
124australia
135Finland
145Italy
155Italy
1610Finland
Data
Cell Formulas
RangeFormula
D1:E7D1=GROUPBY(B1:B100,A1:A100,SUM,3,0,,B1:B100<>"")
Dynamic array formulas.
 
Upvote 0
Solution
That is excellent. I have got to read up on it, as to how that works.

I would never have thought that.
 
Upvote 0
I think =GROUPBY(B1:B16, A1:A16, SUM,3,0) works well for me.

But what effect does the following do
,,B1:B100<>""
 
Upvote 0
Here's a VBA solution:

VBA Code:
Sub test1()
Dim dic As Object, MyTable As Variant, i As Long

    Set dic = CreateObject("Scripting.Dictionary")
    MyTable = Range("A2:B16").Value
    
    For i = 1 To UBound(MyTable)
        dic(MyTable(i, 2)) = dic(MyTable(i, 2)) + MyTable(i, 1)
    Next i
    
    Range("G1:H1") = Array("Grand Total", "Country")

    Range("G2").Resize(dic.Count).Value = WorksheetFunction.Transpose(dic.items)
    Range("H2").Resize(dic.Count).Value = WorksheetFunction.Transpose(dic.keys)
    
End Sub


The Scripting DIctionary works well to find unique values. Make sure to change the input and output ranges to match your sheet.
 
Upvote 0
It filters the data to only show rows where col B is not blank.
In using the code in my real application
VBA Code:
=GROUPBY(B1:B1149,A1:A1149,SUM,3,0,,B1:B1149<>"")
It runs into a problem, as it stops processing values 'Product Quantity' from A33.
It completes filling in all unique 'Product Name' in column B, but all values 'Product Quantity' from A33 are set to 0.

Screenshot 2025-03-25 151937.png


Screenshot 2025-03-25 151954.png
 
Upvote 0
That looks as though the values in A34 & below are actually text & not numbers.
Are they manually entered or a formula?
 
Upvote 0
Yes you are correct.

In checking the formatting, from A2 to A1149 they all show as 'general', but only A34 to A46 have 'top left error green triangle'

This data was exported from a stock control program, but not sure why A1 to A33 were processed correctly, as they are still formatted 'general'.

So am I right in assuming that even though a cells formatting shows as 'general' it can still be either a number or text

It all works fine now.

Very much appreciated, thanks
 
Upvote 0

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