count unique semin comma separated words in cell

peter_sjogarde

Board Regular
Joined
Feb 13, 2012
Messages
56
I want to count unique occurrences of a word in a cell. The words are separated with semi comma and the number of words in each cell varies. The cell could be empty.

Example

Column A
Sweden;USA;Sweden
USA,Norway,Canada
USA,USA
[emptycell]

I would like the answer in column B, which should be:
2
3
1
0

I am thankful for any help!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi Peter

Try this udf to count duplicate words in a semicolon separated list of words.

Assumption: a word is a sequence of non-semicolon characters delimited by a semicolon or the start or end of line


Code:
Function CountUniq(s As String) As Long
Dim coll As Collection
Dim v As Variant, vE As Variant
 
Set coll = New Collection
v = Split(s, ";")
On Error Resume Next
For Each vE In v
    coll.Add 1, vE
Next vE
CountUniq = coll.Count
End Function
 
Upvote 0
Hi Peter

Try this udf to count duplicate words in a semicolon separated list of words.

Assumption: a word is a sequence of non-semicolon characters delimited by a semicolon or the start or end of line


Code:
Function CountUniq(s As String) As Long
Dim coll As Collection
Dim v As Variant, vE As Variant
 
Set coll = New Collection
v = Split(s, ";")
On Error Resume Next
For Each vE In v
    coll.Add 1, vE
Next vE
CountUniq = coll.Count
End Function


Nice function pgc1. What if in the OP's example you wanted to count all the uniques in A1:A4? The answer would be 4: Canada, Norway, Sweden, USA. Can the UDF be modified to do that?
 
Upvote 0

Forum statistics

Threads
1,215,330
Messages
6,124,305
Members
449,150
Latest member
NyDarR

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