如何计算文本文件中Word的出现次数 [英] How to Count the Occurrences of a Word in a Text File
本文介绍了如何计算文本文件中Word的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有VBA代码循环遍历多个文件夹中的多个文本文件。 此代码工作正常。 我无法弄清楚的是如何计算每个文本文件中特定单词的出现次数。 有人可以发布一些代价可以
吗?
I have VBA code that loops through multiple text files in multiple folders. This code works fine. What I can't really figure out is how to count the occurrences of a specific word in each text file. Can someone post some code that would do this?
谢谢!
知识是唯一的东西我可以给你,但仍然保留,我们两个都更好。
Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.
推荐答案
调整类似下面的内容(警告只进行轻微测试)
Adapt something like the following (warning only lightly tested)
Sub test()Dim i As LongDim s As String, sText As String s = "Apples and pears, applepear pie, a green apple and red Apple. " For i = 1 To 100 sText = sText & s NextDebug.Print CountWord(sText, "apple", bWhole:=True, bMatchCase:=True) '1Debug.Print CountWord(sText, "apple", bWhole:=True, bMatchCase:=False) '2Debug.Print CountWord(sText, "apple", bWhole:=False, bMatchCase:=True) '2Debug.Print CountWord(sText, "apple", bWhole:=False, bMatchCase:=False) '4End SubFunction CountWord(ByVal sText As String, ByVal sWord As String, _ bWhole As Boolean, bMatchCase As Boolean) As LongDim b As BooleanDim pos As Long, pos2Dim cnt As LongDim nLgth As LongConst cIgnore As String = " '(),-./:;<>?@_`‚„…‹‘’""–—˜«¯" If bMatchCase = False Then sText = UCase
(sText)sWord = UCase
(sText) sWord = UCase
(sWord)结束如果nLgth = Len(sWord)pos = 1而pos pos = InStr(pos,sText,sWord)If pos then b = True pos2 = pos + nLgth if bWhole then b = True如果pos> 1然后b = InStr(1,cIgnore,Mid
(sWord) End If nLgth = Len(sWord) pos = 1 While pos pos = InStr(pos, sText, sWord) If pos Then b = True pos2 = pos + nLgth If bWhole Then b = True If pos > 1 Then b = InStr(1, cIgnore, Mid
这篇关于如何计算文本文件中Word的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文