Count unique characters

jts2004

Board Regular
Joined
Apr 21, 2004
Messages
156
ex: cell A1 has the text "AABBCCDDEFGAB" I want to count the "A"'s , "B"'s ect...
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
If you want to count the number of unique characters try this UDF:

Code:
Function UniqueChars(Str As String)
    Dim i As Integer
    Dim Chars As New Collection
    On Error Resume Next
    For i = 1 To Len(Str)
        Chars.Add Mid(Str, i, 1), Mid(Str, i, 1)
    Next i
    UniqueChars = Chars.Count
End Function

Use it on your worksheet like:

=UniqueChars(A1)
 
Upvote 0
Here is a copy of a small worksheet .

aaabbbccccddddeeeeffff
3 a =REGEX.COUNT($A$1,"a")
3 b =REGEX.COUNT($A$1,"b")
4 c =REGEX.COUNT($A$1,"b")
4 d =REGEX.COUNT($A$1,"e")
4 e =REGEX.COUNT($A$1,"f")
3 a =REGEX.COUNT(A1,"a",FALSE)
4 b =REGEX.COUNT($A$1,"b",FALSE)
4 c =REGEX.COUNT($A$1,"c",FALSE)
4 d =REGEX.COUNT($A$1,"d",FALSE)
4 e =REGEX.COUNT($A$1,"e",FALSE)
4 f =REGEX.COUNT($A$1,"f",FALSE)

The REGEX.COUNT is part of the Morefunc Excel addin you can find the link below. There is true function that checks for upper or lower case or a false that will count both.


http://www.mrexcel.com/forum/showthread.php?t=126629


I hope that helps

mike in wisconsin
 
Upvote 0
Hi, Perhaps a bit late, but this will give all results in columns "C & D".
Code:
[COLOR="Navy"]Sub[/COLOR] MG26Jan32
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] n = 1 To Len([a1])
    [COLOR="Navy"]If[/COLOR] Not .Exists(Mid([a1], n, 1)) [COLOR="Navy"]Then[/COLOR]
        .Add Mid([a1], n, 1), 1
    [COLOR="Navy"]Else[/COLOR]
        .Item(Mid([a1], n, 1)) = .Item(Mid([a1], n, 1)) + 1
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
Range("C1").Resize(.Count, 2) = Application.Transpose(Array(.Keys, .items))
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,150
Members
453,021
Latest member
Justyna P

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