代码如下:
Option Explicit
Sub test()
Dim i As Long, mx As Long, tmp As Long, res As Long
mx = 0
For i = 0 To 9
tmp = Application.WorksheetFunction.CountIf(Sheets("Sheet1").Range("A:A"), i)
'代码是统计Sheet1表A列中出现最多的数,可以根据自己的需要更改Sheets("Sheet1").Range("A:A")就可以了
If tmp > mx Then mx = tmp: res = i
Next i
MsgBox "出现最多的是:" & res & vbCrLf & "共出现次数:" & mx
End Sub
其实不用这么麻烦的,一个数组公式就可以搞定,如下图数组公式:
=MAX(COUNTIF(A:A,{0,1,2,3,4,5,6,7,8,9})) 然后同时按ctrl+shift+enter组合键结束公式编辑!