Function to count unique characters within a range

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.

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
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
How about

Code:
Function cSingles(rng As Range)
Dim c As Range, n As Long
For Each c In rng
    If WorksheetFunction.CountIf(Range(c, Cells(rng.Cells(1).Row + rng.Count - 1, 1)), c) = 1 Then n = n + 1
Next
cSingles = n
End Function
 
Upvote 0
Solution
Tested?

Explain for all please!

Thanks for your contribuition!

How about

Code:
Function cSingles(rng As Range)
Dim c As Range, n As Long
For Each c In rng
    If WorksheetFunction.CountIf(Range(c, Cells(rng.Cells(1).Row + rng.Count - 1, 1)), c) = 1 Then n = n + 1
Next
cSingles = n
End Function
 
Last edited:
Upvote 0
Tested?

Explain for all please!

Thanks for your contribuition!

Of course.
I invite you to take a test and tell me.

Basically it does the same thing, a cycle for each data of the range.

Code:
[COLOR=#333333]For Each c In rng[/COLOR]


Counts the number of values ​​from the cell to the end of the range. If the account is 1, then it is unique and adds 1 to the counter.


Code:
[COLOR=#333333][I]Range(  from c, to end of range )[/I][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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