Excel Formula Countifs + Unique Values

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
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
 
If I wanted the VBA to just produce the value
Do you mean not have it as a function, but just a one-off calculation?

VBA Code:
Sub UniqueCount()
  Dim d As Object
  Dim a As Variant, Ky As Variant
  Dim lastrw As Long, i As Long
  Dim s As String
  
  Const CritColValCol As String = "8 5" '<- Criteria column & Values column in that order. Edit to suit.
  Const ResultsTopLeft As String = "K1" '<- Where you want the results
  
  lastrw = Cells(Rows.Count, CLng(Split(CritColValCol)(0))).End(xlUp).Row
  a = Application.Index(Cells, Evaluate("row(2:" & lastrw & ")"), Split(CritColValCol))
  Set d = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(a)
    s = "|" & a(i, 2) & "|"
    If InStr(1, d(a(i, 1)), s, 1) = 0 Then d(a(i, 1)) = d(a(i, 1)) & s
  Next i
  ReDim a(1 To d.Count, 1 To 2)
  i = 0
  For Each Ky In d.Keys()
    i = i + 1
    a(i, 1) = Ky: a(i, 2) = UBound(Split(d(Ky), "||")) + 1
  Next Ky
  With Range(ResultsTopLeft)
    .Resize(, 2).Value = Array("Criteria", "Count")
    .Offset(1).Resize(d.Count, 2).Value = a
  End With
End Sub

ItalianPlatinum 2.xlsm
EFGHIJKL
1ValuesCriteriaCriteriaCount
2ABCAAAA3
3ABCAABB2
4DEFAACC2
5ABC1BB
6DEF1BB
7ABC2CC
8ABC2CC
9ABC2CC
10DEF2CC
11XXXAA
Sheet1
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Do you mean not have it as a function, but just a one-off calculation?

VBA Code:
Sub UniqueCount()
  Dim d As Object
  Dim a As Variant, Ky As Variant
  Dim lastrw As Long, i As Long
  Dim s As String
 
  Const CritColValCol As String = "8 5" '<- Criteria column & Values column in that order. Edit to suit.
  Const ResultsTopLeft As String = "K1" '<- Where you want the results
 
  lastrw = Cells(Rows.Count, CLng(Split(CritColValCol)(0))).End(xlUp).Row
  a = Application.Index(Cells, Evaluate("row(2:" & lastrw & ")"), Split(CritColValCol))
  Set d = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(a)
    s = "|" & a(i, 2) & "|"
    If InStr(1, d(a(i, 1)), s, 1) = 0 Then d(a(i, 1)) = d(a(i, 1)) & s
  Next i
  ReDim a(1 To d.Count, 1 To 2)
  i = 0
  For Each Ky In d.Keys()
    i = i + 1
    a(i, 1) = Ky: a(i, 2) = UBound(Split(d(Ky), "||")) + 1
  Next Ky
  With Range(ResultsTopLeft)
    .Resize(, 2).Value = Array("Criteria", "Count")
    .Offset(1).Resize(d.Count, 2).Value = a
  End With
End Sub

ItalianPlatinum 2.xlsm
EFGHIJKL
1ValuesCriteriaCriteriaCount
2ABCAAAA3
3ABCAABB2
4DEFAACC2
5ABC1BB
6DEF1BB
7ABC2CC
8ABC2CC
9ABC2CC
10DEF2CC
11XXXAA
Sheet1
if the data references another sheet or workbook could i just add With sheets in order to pull it? so issue i was seeing I was using INDIRECT formula in combination with your previous formula but using indirect the workbook had to be open to calculate so that is where my most recent request came from sorry for the multiple back and forths
 
Upvote 0
I would expect using INDIRECT on such a large data set could have a significant impact on your sheet's performance?
My code could reference data on another sheet or in another workbook but that other workbook would still need to be open.
 
Upvote 0
I would expect using INDIRECT on such a large data set could have a significant impact on your sheet's performance?
My code could reference data on another sheet or in another workbook but that other workbook would still need to be open.
I have not tested it yet. so the workbook will be open but at the end will be closed. using indirect it was calculating but then once the file was closed it would kick #Value! this is what made me think i need it to calculate in VBA and just put the value in the cell?
 
Upvote 0
How could i reference another sheet by using the below:

VBA Code:
Const CritColValCol As String = "8 5" '<- Criteria column & Values column in that order. Edit to suit.
 
Upvote 0
HI, I just want to understand your requirement. can you please explain. how we will get the output from given source data.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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