查询:我想计数某个单词在excel单元格中出现的次数。
如果可能的话,可以使用一个脚本来总结excel单元格范围内每个单词出现的次数,而不是寻找特定的单词。也就是说,按照最高到最低的频率来概括,每个单词出现的次数。
Excel文件:
每个单元格都有>1000 characters
谢谢!
发布于 2021-03-22 12:59:24
首先,需要一个函数来删除从单元格中获得的字符串中的所有特殊字符。
Function without_special_chars(text As String) As String
Dim i As Integer
Const special_chars As String = "-.,:;#+ß'*?=)(/&%$§!~\}][{"
For i = 1 To Len(special_chars)
text = Replace(text, Mid(special_chars, i, 1), "")
Next i
without_special_chars = text
End Function
这只是一个查看字符串是否已经在数组中的函数。
Function IsInArray(stringToBeFound, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function
然后你就可以遍历所有的细胞了。
Sub query()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Tabelle1")
Dim wordlist()
Dim nList()
Dim word As String
For i = 1 To ws.Cells(1048576, 11).End(xlUp).Row
text = without_special_chars(ws.Cells(i, 11).Value)
cell_words = Split(text, " ")
For j = 0 To UBound(cell_words)
the_word = cell_words(j)
If j = 0 And i = 1 Then
ReDim wordlist(0)
ReDim nList(0)
wordlist(UBound(wordlist)) = the_word
nList(UBound(nList)) = 1
Else
If IsInArray(the_word, wordlist) = True Then
For n = 0 To UBound(wordlist)
If wordlist(n) = the_word Then
nList(n) = nList(n) + 1
Exit For
End If
Next
Else
ReDim Preserve wordlist(UBound(wordlist) + 1)
ReDim Preserve nList(UBound(nList) + 1)
wordlist(UBound(wordlist)) = the_word
nList(UBound(wordlist)) = 1
End If
End If
Next
Next
For m = 0 To UBound(wordlist)
ws.Cells(m + 1, 1).Value = wordlist(m)
ws.Cells(m + 1, 2).Value = nList(m)
Next
End Sub
对于ws,您必须将其写在工作表的名称中。最后一个使用m的for -循环是输出,我只是为测试目的而做的,最终您将有两个数组。单词列表存储单词,nList存储数量。我没有考虑大小写,所以有可能有两个条目“我”和“我”。
您只需将这些代码放在VBA编辑器中并运行查询子。(打开Excel,按ALT+F11,将其放置在ThisWorkbook中,单击其中一行,然后单击run)
VBA不是很快,所以我不知道它需要多长时间,但是我用几百个单元格和一些句子测试了它,它需要大约1,5分钟。
https://stackoverflow.com/questions/66742744
复制相似问题