这个基本上只有用宏来做了。我写了一个自定义函数。用法如下:
=MyCount(A1:A5),=MyCount(A1:A5,B4:B5) 都行的。括号中的参数就是需要统计的单元格。
-------------如下是代码---------------
Option Explicit
Option Base 1
Function MyCount(ParamArray SomeThing()) As String
Dim myResult(10, 2) As Integer, tmpStr As String
Dim m1, m2 As Range
For Each m1 In SomeThing
For Each m2 In m1
'单个检查字符情况,放到tmpArr中
tmpStr = CountOne(m2.Value)
'将tmpArr中结果并入myResult中
Call CountAdd(myResult, tmpStr)
Next m2
Next m1
MyCount = GetResult(myResult) '从myResult中选出结果
End Function
Private Function CountOne(tmpStr As String) As String
Dim i As Integer, tmp As String
CountOne = ""
For i = 1 To Len(tmpStr)
tmp = Mid(tmpStr, i, 1)
If tmp >= "0" And tmp <= "9" Then '非数字忽略
If InStr(1, CountOne, tmp) = 0 Then CountOne = CountOne & tmp
End If
Next i
End Function
Private Function CountAdd(myResult() As Integer, tmpStr As String)
Dim i As Integer, j As Integer, tmp As String
Dim WriteOK As Boolean
For i = 1 To Len(tmpStr)
tmp = Mid(tmpStr, i, 1)
WriteOK = False
For j = 1 To UBound(myResult, 1)
If myResult(j, 1) = 0 And myResult(j, 2) = 0 Then Exit For
If CInt(tmp) = myResult(j, 1) Then
myResult(j, 2) = myResult(j, 2) + 1
WriteOK = True
End If
Next j
If Not WriteOK Then
myResult(j, 1) = tmp
myResult(j, 2) = 1
End If
Next i
End Function
Private Function GetResult(myResult() As Integer) As String
Dim max As Integer, i As Integer
max = 0
For i = 1 To UBound(myResult, 1)
If myResult(i, 1) = 0 And myResult(i, 2) = 0 Then Exit For
If myResult(i, 2) > max Then max = myResult(i, 2)
Next i
GetResult = ""
For i = 1 To UBound(myResult, 1)
If myResult(i, 1) = 0 And myResult(i, 2) = 0 Then Exit For
If myResult(i, 2) = max Then GetResult = GetResult & CStr(myResult(i, 1))
Next i
End Function
有疑问,请Hi我或给我发百度消息
GoodLuck!
温馨提示:内容为网友见解,仅供参考