|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub 统计各单词出现的次数() '将Word文档与Excel文件放同一目录
- Dim c$, s$, d, d1, k, i&, j&, temp$, reg, Match, Matches, arr, brr(), wb
- s = ActiveDocument.Content.Text
- Set d = CreateObject("Scripting.Dictionary")
- Set d1 = CreateObject("Scripting.Dictionary")
- Set wb = CreateObject("Excel.Application")
- d.CompareMode = vbTextCompare '不区分大小写
- Set reg = CreateObject("VBScript.RegExp")
- With reg
- .Pattern = "[A-Za-z]+"
- .Global = True
- Set Matches = .Execute(s)
- For Each Match In Matches
- With Match
- If d.Exists(.Value) Then d(.Value) = d(.Value) + 1 Else d.Add .Value, 1
- End With
- Next
- k = d.Keys
- For i = 0 To UBound(k) - 1
- For j = i + 1 To UBound(k)
- If d(k(i)) < d(k(j)) Then
- temp = k(i)
- k(i) = k(j)
- k(j) = temp
- End If
- Next
- Next
- ReDim brr(1 To UBound(k) + 2, 1 To 5)
- j = 1
- brr(j, 1) = "单词": brr(j, 2) = "频次": brr(j, 3) = "中文": brr(j, 4) = "音标": brr(j, 5) = "课文来源"
- For i = 0 To UBound(k)
- j = j + 1
- brr(j, 1) = k(i)
- brr(j, 2) = d(k(i)) & "次"
- Next
- End With
- With wb.WorkBooks.Open(ActiveDocument.Path & "\单词表.xls")
- With .Sheets("全部")
- arr = .UsedRange.Value
- For i = 2 To UBound(arr)
- d1(LCase(arr(i, 1))) = Array(arr(i, 2), arr(i, 3), arr(i, 4))
- Next
- End With
- With .Sheets("Sheet1")
- .UsedRange.ClearContents
- For i = 2 To UBound(brr)
- If d1.Exists(LCase(brr(i, 1))) Then
- brr(i, 3) = d1(LCase(brr(i, 1)))(0)
- brr(i, 4) = d1(LCase(brr(i, 1)))(1)
- brr(i, 5) = d1(LCase(brr(i, 1)))(2)
- End If
- Next
- .[A1].Resize(UBound(brr), 5) = brr
- End With
- .Close True
- End With
- wb.Quit
- Set wb = Nothing
- MsgBox "单词频次统计完成!"
- End Sub
复制代码
|
|