改程序,不重复统计字数的程序,救助!

来源:百度知道 编辑:UC知道 时间:2024/05/29 01:12:44
求教:word中有时要用到字数统计功能,简单统计很容易,这里要的是不重复的统计,网上搜索,看到已有朋友提过此问题,这里我已在网上找到高手编的vba运行码,但它是统计中文的,我试过,管用,但统计英文怎么改一下呢,应当改动不大即可,可惜自已是个菜鸟, 一点不懂,求懂的朋友把它改一下以能用于不重复地统计英文字数,谢了!

Sub 不重复字符知多少()
Dim AllText As String, B As String
Dim i As Long, StartTime As Single
Dim OnlyText As String
StartTime = Timer
AllText = ActiveDocument.Content.Text
For i = 1 To Len(AllText)
B = Mid(AllText, i, 1)
If B Like "[一-龥]" Then '如果属于汉字字符(CJK统一汉字字符集)
If OnlyText = "" Then
OnlyText = B '为变量赋初值
ElseIf VBA.InStr(OnlyText, B) = 0 Then
'如果在OnlyText变量中,没有出现过的字符,则加入该变量字符集中
OnlyText = OnlyText & B
End If
End If
Next
MsgBox Len(OnlyText), vbOKOnly, Timer - StartTime
End Sub

帖子出处:http://club.excelhome.net/dispbbs.asp?BoardID=23&ID=118832
谢谢伤心小y朋友的回答,但我恐怕不对。

'可以根据英文字符的asc值判断啊
Sub 不重复字符知多少()
Dim AllText As String, B As String
Dim i As Long, StartTime As Single
Dim OnlyText As String
StartTime = Timer
AllText = ActiveDocument.Content.Text
For i = 1 To Len(AllText)
B = Mid(AllText, i, 1)

If (Asc(B) >= 65 And Asc(B) <= 90) Or (Asc(B) >= 97 And Asc(B) <= 122) Then
If OnlyText = "" Then
OnlyText = B '为变量赋初值
Else
'如果在OnlyText变量中,没有出现过的字符,则加入该变量字符集中
OnlyText = OnlyText & B
End If
End If
Next
MsgBox Len(OnlyText), vbOKOnly, Timer - StartTime
End Sub