Excel Formula Countifs + Unique Values

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
782
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - looking for a formula or VBA to accomplish the below. something that will count unique values by a criteria I define

ABCDFGHIJ
XXXXAAABCXXXXXXXXXX
XXXXAAABCXXXXXXXXXX
XXXXAADEFXXXXXXXXXX
XXXXBBABC1XXXXXXXXXX
XXXXBBDEF1XXXXXXXXXX
XXXXCCABC2XXXXXXXXXX
XXXXCCABC2XXXXXXXXXX
XXXXCCABC2XXXXXXXXXX
XXXXCCDEF2XXXXXXXXXX
UniqueAA2
UniqueBB2
UniqueCC2
 

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.
Here is one way:
Book1
ABCDEFGHI
1XXXXAAABCXXXXXXXXXX
2XXXXAAABCXXXXXXXXXX
3XXXXAADEFXXXXXXXXXX
4XXXXBBABC1XXXXXXXXXX
5XXXXBBDEF1XXXXXXXXXX
6XXXXCCABC2XXXXXXXXXX
7XXXXCCABC2XXXXXXXXXX
8XXXXCCABC2XXXXXXXXXX
9XXXXCCDEF2XXXXXXXXXX
10
11UniqueAA2
12UniqueBB2
13UniqueCC2
Sheet1
Cell Formulas
RangeFormula
D11:D13D11{=SUM(IF(FREQUENCY(IF($C$1:$C$9=C11,MATCH($D$1:$D$9,$D$1:$D$9,0)),ROW($C$1:$C$9)-ROW($C$1)+1),1))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
Here is one way:
Book1
ABCDEFGHI
1XXXXAAABCXXXXXXXXXX
2XXXXAAABCXXXXXXXXXX
3XXXXAADEFXXXXXXXXXX
4XXXXBBABC1XXXXXXXXXX
5XXXXBBDEF1XXXXXXXXXX
6XXXXCCABC2XXXXXXXXXX
7XXXXCCABC2XXXXXXXXXX
8XXXXCCABC2XXXXXXXXXX
9XXXXCCDEF2XXXXXXXXXX
10
11UniqueAA2
12UniqueBB2
13UniqueCC2
Sheet1
Cell Formulas
RangeFormula
D11:D13D11{=SUM(IF(FREQUENCY(IF($C$1:$C$9=C11,MATCH($D$1:$D$9,$D$1:$D$9,0)),ROW($C$1:$C$9)-ROW($C$1)+1),1))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
Am I able to use that if it references another workbook that could be closed?
 
Upvote 0
Am I able to use that if it references another workbook that could be closed?
Alright got it to work with an external file. problem is my data has 500,000 lines and this formula is clocking big time. something i should have said earlier of what my actual data set was :)
 
Upvote 0
Maybe someone can help you with a VBA solution. Any formula solution will take time to run on that many rows.
 
Upvote 0
Haven't done much testing for speed on a large range but you could try this user-defined function and compare.
Function assumes 2 adjacent columns with criteria in the left column and values in the right column, like your sample.

VBA Code:
Function CountUnique(rData As Range, sCrit As String) As Long
  Dim d As Object
  Dim a As Variant
  Dim i As Long

  Set d = CreateObject("Scripting.Dictionary")
  a = rData.Value
  For i = 1 To UBound(a)
    If a(i, 1) = sCrit Then d(a(i, 2)) = Empty
  Next i
  CountUnique = d.Count
End Function

ItalianPlatinum 2.xlsm
ABCDEFGHI
1XXXXAAABCXXUniqueAA2
2XXXXAAABCXXUniqueBB2
3XXXXAADEFXXUniqueCC2
4XXXXBBABC1XX
5XXXXBBDEF1XX
6XXXXCCABC2XX
7XXXXCCABC2XX
8XXXXCCABC2XX
9XXXXCCDEF2XX
10
Sheet1
Cell Formulas
RangeFormula
I1:I3I1=CountUnique(C$1:D$500000,H1)
 
Upvote 0
Haven't done much testing for speed on a large range but you could try this user-defined function and compare.
Function assumes 2 adjacent columns with criteria in the left column and values in the right column, like your sample.

VBA Code:
Function CountUnique(rData As Range, sCrit As String) As Long
  Dim d As Object
  Dim a As Variant
  Dim i As Long

  Set d = CreateObject("Scripting.Dictionary")
  a = rData.Value
  For i = 1 To UBound(a)
    If a(i, 1) = sCrit Then d(a(i, 2)) = Empty
  Next i
  CountUnique = d.Count
End Function

ItalianPlatinum 2.xlsm
ABCDEFGHI
1XXXXAAABCXXUniqueAA2
2XXXXAAABCXXUniqueBB2
3XXXXAADEFXXUniqueCC2
4XXXXBBABC1XX
5XXXXBBDEF1XX
6XXXXCCABC2XX
7XXXXCCABC2XX
8XXXXCCABC2XX
9XXXXCCDEF2XX
10
Sheet1
Cell Formulas
RangeFormula
I1:I3I1=CountUnique(C$1:D$500000,H1)
Pete - So this works great, I have one curveball though, if the criteria is in column C but the data to count uniquely is in column E how do I adjust for that? I tried to expand the formula but it doesn't yield correctly. I am trying to replicate this for another data set here the data isn't next to each other
 
Upvote 0
Try this, enter the criteria range and values range separately as in this version of the function. It wouldn't even matter if the values range was to the left of the criteria range, so long as you still enter the criteria range as the first argument of the function.

VBA Code:
Function CountUnique(rCrit As Range, rValues As Range, sCrit As String) As Variant
  Dim d As Object
  Dim a As Variant, b As Variant
  Dim i As Long
  
  a = rCrit.Value
  b = rValues.Value
  If UBound(a) = UBound(b) Then
    Set d = CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(a)
      If a(i, 1) = sCrit Then d(b(i, 1)) = Empty
    Next i
    CountUnique = d.Count
  Else
    CountUnique = CVErr(xlErrRef)
  End If
End Function

ItalianPlatinum 2.xlsm
EFGHIJKLM
1ValuesCriteriaCriteriaUnique Count
2ABCAAAA2
3ABCAABB2
4DEFAACC2
5ABC1BB
6DEF1BB
7ABC2CC
8ABC2CC
9ABC2CC
10DEF2CC
Sheet1
Cell Formulas
RangeFormula
M2:M4M2=CountUnique(I$2:I$10,E$2:E$10,L2)
 
Upvote 0
Sorry for not getting back sooner. This seems to be working. If I wanted the VBA to just produce the value is that a complete different undertaking? What I am seeing is its sourcing through a whole lot of data for the formula so seeing some performance latency. seeing i have it sourcing a data file with 400,000 lines
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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