|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 相见是缘8 于 2019-5-25 14:56 编辑
转一个老师的代码给你:
Sub 统计文档中不同汉字出现的次数()
'可统计文档中共有多少个不同的汉字,以及不同汉字出现的次数,但繁体古文中有的汉字不能被统计到。
Dim AChar, tChar As String
Const MaxChar = 3000
Dim Chars(MaxChar) As String
Dim Freq(MaxChar) As Integer
Dim CharNum As Integer
Dim Found As Boolean
Dim i, j, k, Temp As Integer
CharNum = 0
Found = False
For Each aCharacter In ActiveDocument.Characters
AChar = aCharacter
If AscB(AChar) > 128 Then
For i = 1 To CharNum
If Chars(i) = AChar Then
Freq(i) = Freq(i) + 1
Found = True
Exit For
End If
Next i
If Not Found Then
CharNum = CharNum + 1
Chars(CharNum) = AChar
Freq(CharNum) = 1
End If
Found = False
End If
Next aCharacter
For i = 1 To CharNum - 1
k = i
For j = i + 1 To CharNum
If Freq(j) > Freq(k) Then k = j
Next j
If k <> i Then
tChar = Chars(i)
Chars(i) = Chars(k)
Chars(k) = tChar
Temp = Freq(i)
Freq(i) = Freq(k)
Freq(k) = Temp
End If
Next i
tmpName = ActiveDocument.AttachedTemplate.FullName
Documents.Add Template:=tmpName, NewTemplate:=False
With Selection
.TypeText "文档中共有" & CharNum & "个不同字符" & vbCrLf
.TypeText "字符名称" & vbTab & "出现的频率" & vbCrLf
For i = 1 To CharNum
.TypeText Text:=Chars(i) & vbTab & Freq(i) & vbCrLf
Next i
End With
End Sub
|
|