ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 2510|回复: 2

如何显示任意单词在文中出现的次数

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-5-14 19:03 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
如何显示任意单词在文中出现的次数.rar (15.86 KB, 下载次数: 5)
选中任意一个单词,运行代码,能显示该单词在文中出现的次数,这样的代码怎么写?
Researchers believe they have solved one of science's greatestmysteries. They think they have found out why a dog's nose is cold. Scientistsfrom universities in Sweden and Hungary have posited that dogs' noses areconstantly cold because they act as "ultra-sensitive heat detectors".Canines can sense the temperature of something before theytouch it. They added that dogs use their noses to detect tiny changes intemperature, such as when possible predators or prey are nearby. Humansgenerally only know something is hot after touching it, often with painfulconsequences. It was common belief that the only reason dogs' noses are cold isto control and regulate their own body temperature.

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-5-14 19:14 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 zzpsx 于 2020-5-14 19:42 编辑

如何检验每个红色下划线的单词都只出现了一次.rar (18.19 KB, 下载次数: 1)
如何检验每个红色下划线的单词都只出现了一次,如果均为唯一值,则不弹窗。如果这些单词出现了两次或多次,就将它们标绿,这样的代码怎么写?
Valentine's Day is not a public holiday. Government offices, stores,schools and other organizations are open as usual. Public transit systems runon their regular schedule. Restaurants may be busier than usual as many people go out for an evening withtheir spouse or partner. Valentine's Day is also a very popular date forweddings.
There are a number of Saints called Valentine who are honored onFebruary 14. The day became associated with romantic love in the Middle Ages inEngland. This may have followed on fromthe Pagan fertility festivals that were heldall over Europe as the winter came to an end. Traditionally, lovers exchanged hand written notes. Commercial cards became available in the mid nineteenth century.
The most common Valentine's Day symbols are the heart, particularlyin reds and pinks, and pictures or models of Cupid. Cupid is usually portrayedas a small winged figure with a bow and arrow. In mythology, he uses his arrowto strike the hearts of people. Peoplewho have fallen in love are sometimessaid to be 'struck by Cupid's arrow. Other symbols of Valentine's Day are couples in loving embraces and the gifts offlowers, chocolate, red roses andlingerie that couples often give each other.


目前找到了这代码https://blog.csdn.net/weixin_44559388/article/details/87396419
Sub 统计各单词出现的次数()    Dim a As String, Dic As Object    Dim myReg As Object, Matches As Object, Match As Object    Dim k, i As Long, j As Long, temp As String, c As String    a = ActiveDocument.Content.TextSet Dic = CreateObject("Scripting.Dictionary")Dic.CompareMode = vbTextCompareSet myReg = CreateObject("VBScript.RegExp")With myReg    .Pattern = "[A-Za-z]+"  '匹配模式:由纯字母组成的字符串    .Global = True    Set Matches = .Execute(a)    For Each Match In Matches  '统计频次        With Match            If Dic.Exists(.Value) Then Dic(.Value) = Dic(.Value) + 1 Else Dic.Add .Value, 1        End With    Next    k = Dic.Keys  '获取各“单词”    For i = 0 To UBound(k) - 1 '排序        For j = i + 1 To UBound(k)            If k(i) > k(j) Then                temp = k(i)                k(i) = k(j)                k(j) = temp            End If        Next    Next    For i = 0 To UBound(k)  '合并以用于输出            c = c & k(i) & vbTab & Dic(k(i)) & Chr(13)        Next        Documents.Add.Content.Text = "共有如下" & Dic.Count & "个英文单词(含频次):" & Chr(13) & c    End WithEnd Sub

TA的精华主题

TA的得分主题

发表于 2020-5-15 10:47 | 显示全部楼层
楼主可试如下代码。代码统计全部单词的出现次数,代码可以适当修改,可达到你想要的结果。
  1. Sub arrangepara()
  2. Dim r As Range

  3. Set r = ActiveDocument.Range
  4. If (r.Characters.Last.Text = vbCr) Then r.End = r.End - 1
  5. sortpara r
  6. End Sub

  7. Function sortpara(r As Range)
  8. Dim sWrd As String
  9. Dim Found As Boolean
  10. Dim N As Integer, i As Integer, j As Integer, k As Integer, WordNum As Integer
  11. N = r.Words.Count
  12. ReDim Freq(N) As Integer
  13. ReDim Words(N) As String
  14. Dim temp As String

  15. i = 1
  16. WordNum = 0
  17. Do While r.Find.Execute(findtext:="<*>", MatchWildcards:=True, Wrap:=wdFindStop) = True
  18.    If i = N Then Exit Do
  19.         Found = False
  20.         For j = 1 To WordNum
  21.                If Words(j) = r.Text Then
  22.                    Freq(j) = Freq(j) + 1
  23.                    Found = True
  24.                    Exit For
  25.                End If
  26.         Next j
  27.         If Not Found Then
  28.             WordNum = WordNum + 1
  29.             Words(WordNum) = r.Text
  30.             Freq(WordNum) = 1
  31.         End If
  32.    i = i + 1
  33. Loop

  34. Set r = ActiveDocument.Range
  35. r.Collapse wdCollapseEnd
  36. r.InsertParagraphBefore
  37. r.Collapse wdCollapseEnd

  38. r.InsertAfter "Occurrence List:"
  39. r.Collapse wdCollapseEnd
  40. r.InsertParagraphBefore
  41. r.Collapse wdCollapseEnd


  42. For j = 1 To WordNum
  43.     r.InsertAfter Words(j) & " (" & Freq(j) & ")" & vbCr
  44. Next j

  45. r.Select
  46. Selection.Sort SortFieldType:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending
  47. r.Font.Color = wdColorAqua

  48. End Function
复制代码

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-28 04:28 , Processed in 0.053241 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表