我正在查找一个公式来列出值的出现次数,只有它们大于2次时才会出现;并将结果显示在图像中。计数多次出现
例如,如果一个值重复2次,则显示为“2”,3次显示“3”。所以如果在该范围内有两个重复的数字,那么它将如下图所示以“32”显示。 (数字之间不需要逗号)。谢谢。
我正在查找一个公式来列出值的出现次数,只有它们大于2次时才会出现;并将结果显示在图像中。计数多次出现
例如,如果一个值重复2次,则显示为“2”,3次显示“3”。所以如果在该范围内有两个重复的数字,那么它将如下图所示以“32”显示。 (数字之间不需要逗号)。谢谢。
下面是一个简单的UDF:
Function mycount(rng As Range) As String
Dim str As String
Dim rngcnt As Range
For Each rngcnt In rng
If InStr("," & str & ",", "," & rngcnt.Value & ",") = 0 Then
If Application.WorksheetFunction.CountIf(rng, rngcnt) > 1 Then
mycount = mycount & Application.WorksheetFunction.CountIf(rng, rngcnt)
str = str & "," & rngcnt
End If
End If
Next rngcnt
End Function
所以你在纸张上的呼叫将是:
=mycount(A2:H2)
然后复制下来。
我得到了它的定义VBA功能。本功能的方式使用字典,因此有必要对个基准添加到“Microsoft脚本运行”(看here)。另外,我使用了一个函数从here
Function Repetitions(rng As Range)
Dim dict As New Scripting.Dictionary
Dim res() As Integer
For aux = 1 To rng.Count
Dim numero As Integer
numero = rng.Cells(1, aux).Value
If Not dict.Exists(numero) Then
dict.Add numero, 1
Else
dict(numero) = dict(numero) + 1
End If
Next aux
Dim result As String
result = ""
For aux = 0 To UBound(dict.Items)
If dict.Items(aux) > 1 Then result = result & dict.Items(aux)
Next aux
While Len(result)
iTemp = 1
Temp = Left(result, 1)
For I = 2 To Len(result)
If StrComp(Mid(result, I, 1), Temp, vbTextCompare) = 0 Then
If StrComp(Mid(result, I, 1), Temp, vbBinaryCompare) = 1 Then
Temp = Mid(result, I, 1)
iTemp = I
End If
End If
If StrComp(Mid(result, I, 1), Temp, vbTextCompare) = 1 Then
Temp = Mid(result, I, 1)
iTemp = I
End If
Next I
Repetitions = Repetitions & Temp
result = Left(result, iTemp - 1) & _
Mid(result, iTemp + 1)
Wend
End Function
,在字符串中的字符排序毕竟,你将能够使用功能的式在Excel中调用它如下例如:
=Repetitions(A2:F2)
我可以用你的公式,'22122111','33322111'和'44411411'得到你的模式。但我认为你需要vba来获得你正在寻找的答案。 –
如果可能,我的第一选择将是一个公式。但我也可以使用vba解决方案。 – Max
如果一个数字可能重复的次数是固定的,那么如果是超长公式,您可以到达那里。 –