EXCEL VBA: 在创建单词列表时计算单词出现次数

EXCEL VBA: Counting word occurence while creating list of words

我需要创建一个在 A 列的所有单元格中使用的单词列表,以及列表中每个单词的出现次数。

到目前为止,我已经能够创建单词列表了。 (通过搜索论坛。) 单词列表是在 B 列中生成的,任何人都可以帮助我编写代码以便它也生成 C 列中的出现次数吗?

谢谢!

Sub Sample()

Dim varValues As Variant
Dim strAllValues As String
Dim i As Long
Dim d As Object

'Create empty Dictionary
Set d = CreateObject("Scripting.Dictionary")

'Create String With all possible Values
strAllValues = Join(Application.Transpose(Range("A1", Range("A" & Rows.Count).End(xlUp))), " ")
strAllValues = Replace(strAllValues, ".", "")
strAllValues = Replace(strAllValues, ",", "")
strAllValues = Replace(strAllValues, "!", "")
strAllValues = Replace(strAllValues, "?", "")
strAllValues = Application.WorksheetFunction.Trim(strAllValues)


'Split All Values by space into array
varValues = Split(strAllValues, " ")

'Fill dictionary with all values (this filters out duplicates)
For i = LBound(varValues) To UBound(varValues)
    d(varValues(i)) = 1
Next i

'Write All The values back to your worksheet
Range("B1:B" & d.Count) = Application.Transpose(d.Keys)

End Sub

我只处理唯一列表并计数。

...
'Fill dictionary with all values (this filters out duplicates)
For i = LBound(varValues) To UBound(varValues)
    d.item(varValues(i)) = d.item(varValues(i)) + 1
Next i

'Write All The values back to your worksheet
Range("B1").resize(d.count, 1) = Application.Transpose(d.Keys)
Range("C1").resize(d.count, 1) = Application.Transpose(d.items)