Excel 列表中最常用的单词
我在Excel中有一个列表,其中一个子集如下所示:Excel 列表中最常用的单词,excel,list,frequency,Excel,List,Frequency,我在Excel中有一个列表,其中一个子集如下所示: Food and Human Nutrition Food and Human Nutrition with Placement Food and Nutrition with Professional Experience Food Marketing and Nutrition Food Marketing and Nutrition with Placement Food, Nutrition and Health 我想在此列表中找到n
Food and Human Nutrition
Food and Human Nutrition with Placement
Food and Nutrition with Professional Experience
Food Marketing and Nutrition
Food Marketing and Nutrition with Placement
Food, Nutrition and Health
我想在此列表中找到n
最常用的单词。我试着用这个公式找到最常用的词:
=INDEX(rng,MODE(MATCH(rng,rng,0)))
问题是它将每个单元格视为单个字符串,并且由于6行中的每一行都不同,因此找不到最常见的单词。我想让它做的是输出“Food”、“Nutrition”和“and”作为最常见的词,然后是“Marketing”、“Placement”、“with”等。如果你知道并想使用VBA,那么这将是一项非常有趣的任务。因此,一些自定义公式,如
=MostCommonWords(范围;可选WordsMember)
将给出以下结果:
这是公式背后的代码:
Public Function MostCommonWords(inputRange As Range, _
Optional NumberOfWords As Long = 1) As String
Dim myCell As Range
Dim inputString As String, tempString As String, myResult As String
Dim myArr As Variant, myKey As Variant
Dim cnt As Long, topNumber As Long
Dim myColl As Object
Set myColl = CreateObject("Scripting.Dictionary")
For Each myCell In inputRange
tempString = LCase(Replace(myCell, ",", ""))
inputString = inputString & " " & tempString
Next myCell
myArr = Split(inputString)
For cnt = LBound(myArr) To UBound(myArr)
If myColl.exists(myArr(cnt)) Then
myColl(myArr(cnt)) = myColl(myArr(cnt)) + 1
Else
myColl.Add myArr(cnt), 1
End If
Next cnt
For cnt = 1 To NumberOfWords
topNumber = 0
myResult = vbNullString
For Each myKey In myColl
If topNumber < myColl(myKey) Then
topNumber = myColl(myKey)
myResult = myKey
End If
Next myKey
MostCommonWords = MostCommonWords & " " & myResult
myColl.Remove myResult
Next cnt
End Function
公共函数最常用的词(输入范围作为范围_
可选NumberOfWords(长度=1)作为字符串
暗淡的迈塞尔山脉
Dim inputString作为字符串,tempString作为字符串,myResult作为字符串
Dim myArr作为变体,myKey作为变体
尺寸cnt尽可能长,顶部编号尽可能长
作为对象的模糊myColl
设置myColl=CreateObject(“Scripting.Dictionary”)
对于输入范围中的每个myCell
tempString=LCase(替换(myCell,,,,))
inputString=inputString&“”&tempString
下一个迈塞尔
myArr=拆分(inputString)
对于cnt=LBound(myArr)到UBound(myArr)
如果myColl.存在(myArr(cnt)),则
myColl(myArr(cnt))=myColl(myArr(cnt))+1
其他的
myColl.添加myArr(cnt),1
如果结束
下一个碳纳米管
对于cnt=1到NumberOfWords
topNumber=0
myResult=vbNullString
对于myColl中的每个myKey
如果topNumber
它是如何工作的
- 它将输入范围中的所有单元格添加到一个名为
的字符串中李>inputString
- 删除逗号,并将所有单词转换为小写
- 使用字典结构,每个单词都作为一个键添加,它的次数作为一个值使用
- 根据需要显示的字数,检查字典上的最大值并显示其键
- 一旦找到最大值,就会从字典中删除该键,从而可以找到第二大值-
myColl.Remove myResult
食物
将显示为不同的单词,如果表格仅在空格上拆分,则建议先删除标点符号(查找/替换) 这是一个VBA宏,它提供了您想要的内容
- 唯一性通过使用dictionary对象进行测试
- 计数是在字典中完成的
- 然后对结果进行排序
'Set Reference to Microsoft Scripting Runtime
Option Explicit
Sub UniqueWordCounts()
Dim wsSrc As Worksheet, wsRes As Worksheet
Dim rSrc As Range, rRes As Range
Dim vSrc As Variant, vRes As Variant
Dim vWords As Variant
Dim dWords As Dictionary
Dim I As Long, J As Long
Dim V As Variant, vKey As Variant
'Assume source data is in column 1, starting at A1
' Could easily be anyplace
Set wsSrc = Worksheets("sheet2")
With wsSrc
Set rSrc = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
End With
'Results to go a few columns over
Set wsRes = Worksheets("sheet2")
Set rRes = rSrc(1, 1).Offset(0, 2)
'Read source data into vba array (for processing speed)
vSrc = rSrc
'Collect individual words and counts into dictionary
Set dWords = New Dictionary
dWords.CompareMode = TextCompare
For I = 1 To UBound(vSrc, 1)
'Split the sentence into individual words
For Each vKey In Split(vSrc(I, 1))
If Not dWords.Exists(vKey) Then
dWords.Add Key:=vKey, Item:=1
Else
dWords(vKey) = dWords(vKey) + 1
End If
Next vKey
Next I
'Size results array
ReDim vRes(0 To dWords.Count, 1 To 2)
'Column headers
vRes(0, 1) = "Word"
vRes(0, 2) = "Count"
'Populate the columns
I = 0
For Each V In dWords.Keys
I = I + 1
vRes(I, 1) = V
vRes(I, 2) = dWords(V)
Next V
'Size results range
Set rRes = rRes.Resize(UBound(vRes, 1) + 1, UBound(vRes, 2))
'Populate, format and sort the Results range
With rRes
.EntireColumn.Clear
.Value = vRes
With .Rows(1)
.Font.Bold = True
.HorizontalAlignment = xlCenter
End With
.EntireColumn.AutoFit
.Sort key1:=.Columns(2), order1:=xlDescending, key2:=.Columns(1), order2:=xlAscending, MatchCase:=False, Header:=xlYes
End With
End Sub
如果Google Sheets formulas是一个选项的话,它会更容易,因为它有SPLIT()和正则表达式。否则,您可以复制到Word,将非Word字符替换为“
^p
”(段落标记),然后在Excel中粘贴回一列单词。或者尝试在internet上搜索类似“online Word frequency counter”@pnuts的内容-OP希望在列表中查找n个最常见的单词。公式就是这样做的。如果两个词的频率相同,则显示其中一个词,而不是两个词。