Count Distinct in VBA

rdw72777

Well-known Member
Joined
Apr 5, 2005
Messages
723
Is there a simple way to count distinct values in a range (not a pivot, just normal cells in a range) via VBA, or do i need to do the SumProduct/Countif function.

Seems odd, i just want to know the number of unique values in a rnage, but i'm spinning my wheels tryign to do this without putting in a worksheetfuntion = Sumproduct....line. Seems like VBA should have a prettier way but its eluding me.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I was just hoping VBA had something similar, I keep searching and seems like not. I really struggle with worksheet functions so try to avoid them...oh well. Thanks.
 
Upvote 0
Macro Recorder + Edited code using Advanced filter!
Code:
Public Sub GetMeUniques()
Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:=Range("B1"), Unique:=True
End Sub
 
Upvote 0
Sadly, my data is in rows across the top of columns. I'm probably makign a mountain out of a molehill. gonna kill off this thread and just ask for help converting to a worksheet function.

Thanks,
 
Upvote 0
Never mind here's another one & you can adopt this for a specific range:
Code:
Public Sub CountUniquesVBA()
Dim ncUnique As New Collection
Dim lCount As Long
On Error Resume Next
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
ncUnique.Add Item:=Range("A" & i).Value, Key:=CStr(Range("A" & i).Value)
Next i
lCount = ncUnique.Count + 1
MsgBox lCount
End Sub
 
Upvote 0
Is there a simple way to count distinct values in a range (not a pivot, just normal cells in a range) via VBA, or do i need to do the SumProduct/Countif function.

Seems odd, i just want to know the number of unique values in a rnage, but i'm spinning my wheels tryign to do this without putting in a worksheetfuntion = Sumproduct....line. Seems like VBA should have a prettier way but its eluding me.


Simple, No...

Try something like

Rich (BB code):
Sub UniqueCount()
Dim lr As Long, UniqueCount As Long, c As String, ws As Worksheet
Set ws = Sheets("Sheet1") 'Specify your sheet name here
c = "A" 'Spcify the column to count unique entries from
 
lr = ws.Cells(Rows.Count, c).End(xlUp).Row
With ws.Range(ws.Cells(1, c), ws.Cells(lr, c))
    .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=.Range("A1:A296"), Unique:=True
    UniqueCount = .SpecialCells(xlCellTypeVisible).Count
    ws.ShowAllData
End With
MsgBox "There are " & UniqueCount & " Unique entries in column " & c
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,267
Members
452,902
Latest member
Knuddeluff

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