Leandroarb
Board Regular
- Joined
- Oct 7, 2014
- Messages
- 157
Hello everyone!
I needed to count the unique characters within a range and set up this simple function.
I share here for two reasons:
1 - Can be used for someone else
2 - Surely, the masters in excel of this forum will improve it infinitely.
Thank you in advance.
I needed to count the unique characters within a range and set up this simple function.
I share here for two reasons:
1 - Can be used for someone else
2 - Surely, the masters in excel of this forum will improve it infinitely.
Thank you in advance.
Code:
Public Function AccountSingles(Rng As Range) 'Counts unique characters within a sequence
Dim strRegs As String 'For a text string of characters
Dim cont As Integer 'Counter
Dim c As Range
For Each c In Rng
If Application.WorksheetFunction.CountIf(Rng, c) > 0 And InStr(strRegs, c) = 0 Then 'If the character is counted inside the range, but if it is not in the sequence where the loop has 'passed ...
cont = cont + 1 'Add one more to the counter
Else 'Otherwise...
cont = cont 'Keeps the counter with the same value
End If
strRegs = strRegs & c & ", " 'Adds the last character to the string
Next
AccountSingles = cont 'At the end, it assigns the total value to the function
End Function