Dear all,
here is the case:
I have some rows with six columns each.
does any body know how to count how many different number in each row?
ex: 1 - 3 - 1 - 2 - 4 - 5 >> 5 unique numbers
1 - 2 - 3 - 2 - 2 - 4 >> 4 unique numbers and so on.
Printable View
Dear all,
here is the case:
I have some rows with six columns each.
does any body know how to count how many different number in each row?
ex: 1 - 3 - 1 - 2 - 4 - 5 >> 5 unique numbers
1 - 2 - 3 - 2 - 2 - 4 >> 4 unique numbers and so on.
Try this:
Code:Public Sub test()
Dim Values() As Single
Dim i As Integer
ReDim Values(5)
For i = 0 To 5
Values(i) = Cells(1, i + 1).Value
Next i
MsgBox ("You have " & different_values(Values) & " values!")
End Sub
Public Function different_values(Values() As Single) As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim Check As Boolean
j = 0
Check = False
Do
For i = j To UBound(Values) - 1
If Values(j) = Values(i + 1) Then
For k = i + 1 To UBound(Values) - 1
Values(k) = Values(k + 1)
Next k
ReDim Preserve Values(UBound(Values) - 1)
Check = True
Exit For
End If
Next i
If Check = True Then
j = 0
Else
j = j + 1
End If
Check = False
Loop Until j = UBound(Values)
different_values = UBound(Values) + 1
End Function