Obtener el valor más frecuente o menos frecuente en Excel

Código

Function IB·ValorFrecuente(Rango As Range, Optional Ingrese·1·para·obtener·el·Valor·Infrecuente As Variant = 0) As Variant
Dim x As Object, y As Range, l As Variant, m As Long, n As String
Set x = CreateObject("Scripting.Dictionary")
If Ingrese·1·para·obtener·el·Valor·Infrecuente = 0 Then
For Each y In Rango
If Not IsEmpty(y.Value) Then
If Not x.Exists(y.Value) Then
x.Add y.Value, 1
Else
x(y.Value) = x(y.Value) + 1
End If
If x(y.Value) > m Then
m = x(y.Value)
n = y.Value
ElseIf x(y.Value) = m Then
n = n & "; " & y.Value
End If
End If
Next y
If Len(n) > 0 Then
IB·ValorFrecuente = n
Else
IB·ValorFrecuente = ""
End If
ElseIf Ingrese·1·para·obtener·el·Valor·Infrecuente = 1 Then
For Each y In Rango
l = y.Value
If Not IsEmpty(l) Then
If x.Exists(l) Then
x(l) = x(l) + 1
Else
x.Add l, 1
End If
End If
Next y
m = Application.WorksheetFunction.Min(x.Items)
For Each l In x
If x(l) = m Then
If n = "" Then
n = l
Else
n = n & "; " & l
End If
End If
Next l
IB·ValorFrecuente = n
Else
IB·ValorFrecuente = " Ingrese 1 para obtener el valor menos frecuente "
End If
End Function

Comentarios