Count unique values with criteria (non-formula solution)

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
Morning guys,

I'm struggling to find a non formula solution to count unique values in a range but with criteria.

The range to count is A2:A5000 and the criteria range is B2:B5000, "Ports".

I know I can do this with a formula but I need to count for every cell in the range and this is going to be time consuming if I use a formula.

Can anyone offer a solution please?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
this is going to be time consuming if I use a formula.
What do you mean?
Perhaps I am not understanding the requirement but this formula takes well less than 0.1 seconds to calculate for me. Is that considered time consuming?

24 01 22.xlsm
ABCD
1
2A261480
3A313
4A54Ports
5A59Ports
6A483
7A443
8A297
9A231
10A274
11A294
12A394
13A61
14A17Ports
15A351Ports
16A309
17A217
18A409
19A22
20A177
21A232Ports
22A86Ports
23A451Ports
24A8
25A382Ports
26A90
27A158Ports
28A90
29A8
30A328
31A471
32A102Ports
33A397
34A409Ports
35A123
36A55
37A316
38A195
39A16Ports
40A234Ports
41A386Ports
42A11Ports
43A311Ports
44A306Ports
45A147Ports
46A95Ports
47A53Ports
48A85
49A364Ports
50A5
Count Unique
Cell Formulas
RangeFormula
D2D2=SUMPRODUCT(--(B2:B5000="ports"),--(MATCH(A2:A5000&"|"&B2:B5000,A2:A5000&"|"&B2:B5000,0)=ROW(A2:A5000)-ROW(A2)+1))


Still, if that isn't fast enough then this is something like 10x faster on my machine.
VBA Code:
Sub CountUniqueWithCriteria()
  Dim d As Object
  Dim a As Variant
  Dim i As Long, Cnt As Long

  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row).Value
  For i = 1 To UBound(a)
    If LCase(a(i, 2)) = "ports" Then d(a(i, 1)) = 1
  Next i
  Range("E2").Value = d.Count
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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