Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
I have tried several solutions to resolve this issue, which are not working.
Public Function AL_CountUnique(rng As Range) As Long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Description:
'This function returns the count of the number of unique cells in the supplied range.
'It does so by creating an empty collection, then going through all the cells in the
'supplied range and, if they are not already contained in the collection, adding them.
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'dim required vars
Dim MyCol As Collection
Set MyCol = New Collection
Dim mycount As Long
Dim found As Boolean
Dim xtext As String
Dim x As Variant, y As Variant
Dim chckrng As Range
'initiate error handling
'we will need to do stuff which, under some circumstances we know will create an error
'we then want to test if an error has occurred as the next line, so we do NOT want an error
'to cause prog execution to go to a routine, but just continue with the next step. Hence,
'"Resume Next"
On Error Resume Next
Set chckrng = Application.Intersect(rng, rng.Worksheet.UsedRange)
mycount = 0
'check each cell individually
For Each x In chckrng
'find a suitable string for each cell
'If the cell is empty, the string is "Empty"
'If it contains text then the string is the text
'If it contains a numerical value, then the string is the text version of the value
If IsEmpty(x) Then
xtext = "Empty"
Else
If VarType(x) And vbString Then
xtext = x.Text
Else
xtext = Trim(Str(x.Value))
End If
End If
'check if this string has been used as a key to the collection
found = False
If mycount > 0 Then
Set y = MyCol(xtext)
If Err.Number = 0 Then
found = True
Else
Err.Clear
End If
End If
'if the key is not found, this value is unique, so add it to the collection
If Not found Then
MyCol.Add x, Key:=xtext
mycount = mycount + 1
End If
Next x
AL_CountUnique = mycount
End Function