计数多个发生

我正在寻找一个公式来列出值的出现,只要它们大于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脚本运行时”的引用(看这里 )。 另外,我使用了一个函数来从这里sortingstring中的字符

 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)