找到5个不同数字的最常见的发生

时间:2016-02-01 01:51:01

标签: excel-vba vba excel

我正在尝试调整代码,不仅可以找到数字中最多的数字,而且还可以找到工作表中5个不同的数字。我现在使用的代码用于一个数字,每次我尝试下一个语句时都会出错。我在Excel 2010中工作

Sub FindOccurance()

Dim Rng As Range
Dim WorkRng As Range
Set dic = CreateObject("scripting.dictionary")
On Error Resume Next
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
xMax = 0
xOutValue = ""
For Each Rng In WorkRng
    xValue = Rng.Value
    If xValue Then
        dic(xValue) = dic(xValue) + 1
        xCount = dic(xValue)
        If xCount > xMax Then
            xMax = xCount
            xOutValue = xValue
        End If
    End If
Next
MsgBox "The most common value is: " & xOutValue & " Appeared " & xMax & "    Times"
End Sub

0 个答案:

没有答案