|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
向各位请教
以下代码是统计word中出现2次或2次以上的字词,现现在想把出现1次的字词也统计列出,应怎样修改?
Sub WordsCountThree()
Dim i As Range, aVar As Variable, aString As String, MyString As String, BS As String
On Error Resume Next
'友情提示
MsgBox "受文档字数和可用内存以及WORD自身限制,您的操作可能会需要一段时间!", _
vbOKOnly + vbExclamation, "Warnning"
Select Case MsgBox("按YES统计字的出现频次,按NO统计词的出现频次,按CANCEL统计字与词!", _
vbYesNoCancel + vbInformation + vbDefaultButton2)
Case vbYes
BS = "字数频次统计列表"
For Each i In Me.Characters '字中循环
If Asc(i) < -2050 And Asc(i) > -20319 Then
If MyString = "" Then GoTo GNY
If InStr(MyString, i.Text & ",") = 0 Then
GNY: aString = i.Text & ","
MyString = MyString & aString
Else
On Error Resume Next '忽略错误
Me.Variables.Add Name:=i.Text '添加文档变量
If Err.Number <> 0 Then '设置错误陷阱
Err.Clear '清除错误
'将次数累加写入
Me.Variables(i.Text).Value = Me.Variables(i.Text).Value + 1
Else
'首次写入文档变量时,其初始值为2
Me.Variables(i.Text).Value = 2
End If
End If
End If
Next
Case vbNo
BS = "词数频次统计列表"
For Each i In Me.Words '词中循环
If i.Characters.Count > 1 Then '按照中文习惯为二个以上为词组
'判断是否为中文字符
If Asc(i.Characters(1)) < -2050 And Asc(i.Characters(1)) > -20319 Then
If MyString = "" Then GoTo GNN '循环初始阶段跳至GX行标签
If InStr(MyString, i.Text & ",") = 0 Then
GNN: aString = i.Text & "," '加入","分隔符以便精确定位
MyString = MyString & aString
Else
On Error Resume Next '忽略错误
Me.Variables.Add Name:=i.Text '添加文档变量
If Err.Number <> 0 Then '设置错误陷阱
Err.Clear '清除错误
'将次数累加写入
Me.Variables(i.Text).Value = Me.Variables(i.Text).Value + 1
Else
'首次写入文档变量时,其初始值为2
Me.Variables(i.Text).Value = 2
End If
End If
End If
End If
Next
Case vbCancel
BS = "字词数频次统计列表"
For Each i In Me.Words '词中循环
'判断是否为中文字符
If Asc(i.Characters(1)) < -2050 And Asc(i.Characters(1)) > -20319 Then
If MyString = "" Then GoTo GNC '循环初始阶段跳至GX行标签
If InStr(MyString, i.Text & ",") = 0 Then
GNC: aString = i.Text & "," '加入","分隔符以便精确定位
MyString = MyString & aString
Else
On Error Resume Next '忽略错误
Me.Variables.Add Name:=i.Text '添加文档变量
If Err.Number <> 0 Then '设置错误陷阱
Err.Clear '清除错误
'将次数累加写入
Me.Variables(i.Text).Value = Me.Variables(i.Text).Value + 1
Else
'首次写入文档变量时,其初始值为2
Me.Variables(i.Text).Value = 2
End If
End If
End If
Next
End Select
aString = "": MyString = "" '重新初始化变量
Application.ScreenUpdating = False '关闭屏幕更新
With Selection
.EndKey unit:=wdStory '移到文档末尾
'作一个区分标记
.InsertAfter vbCrLf & "------------------------------------" & BS & " ------------------------------------" & vbCrLf
.EndKey unit:=wdStory '移到文档末尾
For Each aVar In Me.Variables '在文档变量中循环
'插入文档中
aString = """" & aVar.Name & """出现频次:" & vbTab & aVar.Value & vbCrLf
MyString = MyString & aString '文本累加写入内存变量中,以加速运行
Next
.InsertAfter MyString
'根据出现频次排序
.Sort FieldNumber:="域 1", SortFieldType:= _
wdSortFieldNumeric, SortOrder:=wdSortOrderDescending
End With
VarClear '清空文档变量
Me.UndoClear '清空撤消
Application.ScreenUpdating = True '恢复屏幕更新
End Sub |
|