Count records in column and output to a table

RFLundgren

New Member
Joined
Mar 28, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi all

I have a sheet with one of the columns containing customer names. I need to loop through the column and find a count of records by customer, to them output to a table.

Customer
Customer 3
Customer 1
Customer 2
Customer 3
Customer 2
Customer 1
Customer 2
Customer 2
Customer 2
Customer 2
Customer 2
Customer 2
Customer 2
Customer 2
Customer 2
Customer 2
Customer 3
Customer 1
Customer 1
Customer 1
Customer 3
Customer 3
Customer 3
Customer 3
Customer 1

I need to output it to a table as below.

CustomerCount of tasks
Customer 16
Customer 212
Customer 33

Number of records changes on a regular basis.

Any help most appreciated. Many thanks
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
See if you could adapt something like this.

VBA Code:
Sub CustCount()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    d(a(i, 1)) = d(a(i, 1)) + 1
  Next i
  Range("C2:D2").Resize(d.Count).Value = Application.Transpose(Array(d.keys, d.Items))
End Sub

My sample data and results:

RFLundgren.xlsm
ABCD
1Customer
2Customer 3Customer 37
3Customer 1Customer 16
4Customer 2Customer 212
5Customer 3
6Customer 2
7Customer 1
8Customer 2
9Customer 2
10Customer 2
11Customer 2
12Customer 2
13Customer 2
14Customer 2
15Customer 2
16Customer 2
17Customer 2
18Customer 3
19Customer 1
20Customer 1
21Customer 1
22Customer 3
23Customer 3
24Customer 3
25Customer 3
26Customer 1
Sheet1
 
Upvote 0
Solution
You could also do this very easily with formulas.

RFLundgren.xlsm
ABCD
1Customer
2Customer 3Customer 37
3Customer 1Customer 16
4Customer 2Customer 212
5Customer 3
6Customer 2
7Customer 1
8Customer 2
9Customer 2
10Customer 2
11Customer 2
12Customer 2
13Customer 2
14Customer 2
15Customer 2
16Customer 2
17Customer 2
18Customer 3
19Customer 1
20Customer 1
21Customer 1
22Customer 3
23Customer 3
24Customer 3
25Customer 3
26Customer 1
Sheet1
Cell Formulas
RangeFormula
C2:C4C2=UNIQUE(A2:A26)
D2:D4D2=COUNTIF(A2:A26,C2#)
Dynamic array formulas.
 
Upvote 0
Hi Peter

Many thanks, words perfectly! It does need to be done in VBA as the number of rows in the sheet can vary greatly from month to month and the result will actually be placed into another workbook.

I may also need to do a variation of this, where it also does a count but based on criteria in a different column. I will see how I go, and pop back if I run into any trouble.

Thanks!
 
Upvote 0
An alternative is to use Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Customer"}, {{"Count", each Table.RowCount(_), Int64.Type}})
in
    #"Grouped Rows"

CustomerCount
Customer 37
Customer 16
Customer 212
 
Upvote 0
Hey Alan

Thanks! Always nice to have alternate options. Also gives me something else to have a look into as have never used Power Query, so something new to learn potentially :)
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,448
Members
448,966
Latest member
DannyC96

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