我需要创建一个用于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
答案 0 :(得分:0)
我将处理唯一列表和计数。
...
'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)