Count Of Types

ellison

Active Member
Joined
Aug 1, 2012
Messages
343
Office Version
  1. 365
Platform
  1. Windows
Hi, am trying to find a way of counting "variations".
The raw data is as per columns A&B, and I'm trying to do a count in Col C of the number of different types listed for each model.


ModelTypeCount-Of-Types
ABC123black1
54123black3
54123143
54123grey3
54123grey3
54123grey3
54123grey3
XYZ789white3
XYZ789red3
XYZ7893
XYZ789123




At the moment, I use a long winded method of:
1. copy original info 2. dedupe 3. pivot table with counts of (only) the models 4. lookup back to the original info 5. hope I haven't left anything off any of the steps 6. try and get back my train of thought (oops!)

I normally use this on pretty big datasets, up to 100K, but there are only ever 2 fields.

The added complications seem to be:
i) although there is always a model, there isn't always a type listed - sometimes they are left blank).
ii) both the model and the type can be alphanuemeric strings, but also some are made entirely of digits and some are just letters

I'd be happy to use helper columns, code or whatever could help to make this a bit quicker / easier / foolproof (especially with my stickY fingers!)

Thanks for looking.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
@ellison Maybe like:

Book1
ABCDE
1ModelTypeCount-Of-TypesHelper
2ABC123black11
354123black31
4541231431
554123grey30.25
654123grey30.25
754123grey30.25
854123grey30.25
9XYZ789white31
10XYZ789red31
11XYZ78930
12XYZ7891231
Sheet1
Cell Formulas
RangeFormula
C2:C12C2=IF(A2="","",SUMIF(A:A,A2,E:E))
E2:E12E2=IFERROR(1/COUNTIFS(A:A,A2,B:B,B2),0)


Hope that helps.
 
Upvote 0
What about this?

ellison.xlsm
ABC
1ModelTypeCount-Of-Types
2ABC123black1
354123black3
454123143
554123grey3
654123grey3
754123grey3
854123grey3
9XYZ789white3
10XYZ789red3
11XYZ7893
12XYZ789123
13ABC9990
Count Types
Cell Formulas
RangeFormula
C2:C13C2=IFERROR(ROWS(UNIQUE(FILTER(B$2:B$13,(A$2:A$13=A2)*(B$2:B$13<>""),NA()))),0)
 
Upvote 0
Hi, both work thanks... and work beautifully on the smaller sets of data.

But both seem to go quite slow on the larger sets of data (eg 100K rows).

???
 
Upvote 0
But both seem to go quite slow on the larger sets of data (eg 100K rows).
Not too surprising. That is a lot of data to process! ;)

  1. Would a vba solution be acceptable?
  2. Is your real data grouped by Model and Type like your sample? .. and if not, could we sort it that way to help processing speed?
 
Upvote 0
Not too surprising. That is a lot of data to process! ;)

  1. Would a vba solution be acceptable?
  2. Is your real data grouped by Model and Type like your sample? .. and if not, could we sort it that way to help processing speed?
Hi peter, sorry.... you’re right, it is a lot of data grr!

Absolutely yes, vba solution would be great.

And yes, the data could be grouped together (although it currently isn’t when it’s in its raw format, it would just be a quick extra step to include).

thanks
 
Upvote 0
Hmm, I probably didn't ask enough questions about the desired results layout etc but you could consider a Pivot Table or try this with a copy of your data.

VBA Code:
Sub Count_Types()
  Dim d As Object
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  Application.ScreenUpdating = False
  Set d = CreateObject("Scripting.Dictionary")
  With Range("A1", Range("A" & Rows.Count).End(xlUp).Offset(1)).Resize(, 2)
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(2), Order2:=xlAscending, Header:=xlYes
    a = .Value
    ReDim b(1 To UBound(a), 1 To 2)
    For i = 2 To UBound(a) - 1
      If Len(a(i, 2)) > 0 Then d(a(i, 2)) = 1
      If a(i, 1) <> a(i + 1, 1) Then
        k = k + 1
        b(k, 1) = a(i, 1)
        b(k, 2) = d.Count
        d.RemoveAll
      End If
    Next i
    .Offset(1, 2).Resize(k, 2).Value = b
  End With
  Application.ScreenUpdating = True
End Sub

For the sample data I used in post #3, this code produced the following:

ellison.xlsm
ABCD
1ModelTypeModelCount
25412314541233
354123blackABC1231
454123greyABC9990
554123greyXYZ7893
654123grey
754123grey
8ABC123black
9ABC999
10XYZ78912
11XYZ789red
12XYZ789white
13XYZ789
14
Count Types (2)
 
Upvote 0
Thanks so much Peter,

I added in a copy of the original data in columns I and J, put in the (original) row number in Col H (just as a double check), then in Col K, I put in a lookup back to the counts produced by your VBA (columns C & D).

I think I can now keep this a template once it's set up and copy and paste info into it to get the counts that we need out quickly & reliably (without my fat fingers wreaking too much haVOc!)

Quick excerpt:

Model-V2Type-V2Model-V3Count-Of-TypesOriginal-Row-NumberOriginal-ModelOriginal-TypeLooked-Up-Count-Of-Variations
54123145412372ABC123black1
5412314ABC1231354123fffff7
5412314ABC1241454123147
5412314ABC1251554123grey7
5412314ABC1261654123p7
5412314ABC1271754123red7
5412314ABC1281854123grey7
5412314ABC12919XYZ789white118
5412314ABC130110XYZ789red118
5412314ABC131111XYZ789118
5412314ABC132112XYZ78912118
5412314ABC133113ABC124black1
5412314ABC13411454123black7
5412314ABC13511554123147


PS Thanks aGain!
 
Upvote 0
You are welcome. Thanks for the follow-up.
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,888
Members
449,097
Latest member
dbomb1414

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