Count number of unique values in a single cell separated by commas

crackod93

Board Regular
Joined
Aug 9, 2007
Messages
71
Hi, probably a very easy one here, im looking to count the unique number of values within a single cell string which are separated by commas

example:

Cell A1 contains: 1,2,3A33,444,2,1,16

The resulting formula would give a count of 5 unique value within cell A1 as values '2' and '1' are repeated twice within the cell and would therefore be excluded.

Any help would be most appreciated, thank you!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Probably the cleanest way to do this would be to create a function in VBA like the one below. Then, your formula would be =UniqueValues(A1). If you were to try to do a one-cell formula that parses the string in A1 into individual tokens and then counts up the unique values, I think you'd have one big hairy formula, assuming it's possible. (And, if it is possible, the members on this forum will do it!)

RGR

Code:
Public Function UniqueValues(InputString As String)
Dim Token() As String
Dim TokenCount, TokenStart, TokenEnd As Integer
Dim UniqueToken As Boolean
    TokenStart = 1
    For c = 1 To Len(InputString)
        If Mid(InputString, c, 1) = "," Then
            TokenCount = TokenCount + 1
            ReDim Preserve Token(TokenCount)
            Token(TokenCount) = Mid(InputString, TokenStart, c - TokenStart)
            UniqueToken = True
            For t = 1 To TokenCount - 1
                If Token(t) = Token(TokenCount) Then
                    UniqueToken = False
                End If
            Next
            If UniqueToken = True Then UniqueValues = UniqueValues + 1
            TokenStart = c + 1
        End If
    Next
    TokenCount = TokenCount + 1
    ReDim Preserve Token(TokenCount)
    Token(TokenCount) = Mid(InputString, TokenStart, c - TokenStart)
    UniqueToken = True
    For t = 1 To TokenCount - 1
        If Token(t) = Token(TokenCount) Then
            UniqueToken = False
        End If
    Next
    If UniqueToken = True Then UniqueValues = UniqueValues + 1
    TokenStart = c + 1
End Function
 
Upvote 0
Hi

This is a formula solution.

I chose to use a name to make the formula smaller

- with B1 selected, define the name

Name: _Arr
Refers to: =EVALUATE("{"""&SUBSTITUTE(Sheet1!A1,",",""",""")&"""}")

- In B1 enter the formula:

=SUM((2+LEN(_Arr))/(2+LEN(SUBSTITUTE(A1,",",",,"))-LEN(SUBSTITUTE(","&SUBSTITUTE(A1,",",",,")&",",","&_Arr&",",""))))

This is an array formula, you have to confirm it with CTRL+SHIFT+ENTER.
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,258
Members
449,149
Latest member
mwdbActuary

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