[英]Count number of words in a cell that are in a range
In one column I have phrases eg Peanut butter jelly time
. 在一栏中我有短语,例如
Peanut butter jelly time
。 I need to know how many words in a range (eg B:B) appear in each phrase. 我需要知道每个短语中出现了一个范围内的几个词(例如B:B)。 It would be awesome to know which words are in the range.
知道范围内的哪个单词真是太棒了。 Case doesn't matter.
大小写无关紧要。
Ideally, the output would look like: 理想情况下,输出如下所示:
Phrase--------------------------NumOfWords----------------Words 词组-------------- NumOfWords ---------------- Words
Peanut butter jelly time----------2-----------------------------butter, time 花生酱果冻时间---------- 2 ---------------------------------黄油,时间
Wayne's World-------------------1-----------------------------world 韦恩的世界------------------- 1 ---------------------------- -世界
I have tried various formulas, both array and non-array, and some UDFs. 我尝试了各种公式,包括数组和非数组,以及一些UDF。
Would prefer a non-VBA solution, since I'm still a novice, but any help is much appreciated. 由于我仍然是新手,因此宁愿使用非VBA解决方案,也请多多帮助。
Try the following User Defined Function (UDF) : 尝试以下用户定义函数 (UDF) :
Public Function WordCounter(phrase As String, llist As String) As Long
Dim s As String, sp As String
Dim mtch As String
WordCounter = 0
sp = " "
s = sp & LCase(Trim(phrase)) & sp
ary = Split(Replace(llist, " ", ""), ",")
For Each a In ary
mtch = sp & a & sp
If InStr(1, s, mtch) > 0 Then
WordCounter = WordCounter + 1
End If
Next a
End Function
The space-padding is used to avoid matching parts of words. 空格填充用于避免单词的匹配部分。
User Defined Functions (UDFs) are very easy to install and use: 用户定义函数(UDF)易于安装和使用:
If you save the workbook, the UDF will be saved with it. 如果您保存工作簿,则UDF将随之保存。 If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx
如果您在2003年以后使用Excel版本,则必须将文件另存为.xlsm而不是.xlsx
To remove the UDF: 删除UDF:
To use the UDF from Excel: 要从Excel使用UDF:
=WordCounter(A1,C1) = WordCounter(A1,C1)
To learn more about macros in general, see: 要总体上了解有关宏的更多信息,请参见:
http://www.mvps.org/dmcritchie/excel/getstarted.htm http://www.mvps.org/dmcritchie/excel/getstarted.htm
and 和
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx http://msdn.microsoft.com/zh-CN/library/ee814735(v=office.14).aspx
and for specifics on UDFs, see: 有关UDF的详细信息,请参见:
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
Macros must be enabled for this to work! 必须启用宏才能使其正常工作!
Your example: 你的例子:
For a possible non-VBA solution, try the following: 对于可能的非VBA解决方案,请尝试以下操作:
named range
that contains a list of single words that you want to determine if they are in the phrase in a particular cell in Column A. named range
,其中包含您要确定单个单词是否在列A的特定单元格中的短语中的列表。 For the number of matching words, try this formula: 有关匹配单词的数量,请尝试以下公式:
EDIT: formulas edited to eliminate partial matches. 编辑:编辑公式以消除部分匹配。
B2: =SUMPRODUCT(--ISNUMBER(SEARCH(" " & WordList & " "," " & A2 & " ")))
For the individual matching words, try this array-entered
formula. 对于单个匹配词,请尝试使用此
array-entered
公式。 (To array-enter
a formula, hold down ctrl+shift
when you hit enter
to confirm the formula. If you do this correctly, Excel will place braces {...}
around the formula: (要
array-enter
公式,请在按enter
时按住ctrl+shift
确认公式。如果正确执行此操作,Excel会在公式周围放置括号{...}
:
C2: =IFERROR(INDEX(" " & WordList & " ",SMALL(ISNUMBER(SEARCH(" " & WordList & " "," " & $A2 & " "))*ROW(WordList),COLUMNS($A:A)+SUM(--NOT(ISNUMBER(SEARCH(" " & WordList & " "," " & $A2 & " ")))))),"")
Then select C2 and fill right for at least as many cells as there are words to return, or until the formula returns blanks. 然后选择C2并向右填充至少与要返回的单词一样多的单元格,或者直到公式返回空白为止。 Then select the cells you just filled, and fill down as far as required.
然后选择刚填充的单元格,并根据需要向下填充。
The formulas in C2 and rightward will only work if the range WordList begins with the first word in Row 1 (could be in any column on any worksheet, but needs to start with Row 1). 仅当范围WordList以行1中的第一个单词开头(可以在任何工作表的任何列中,但需要以行1开头)时,C2中的公式才有效。 If it starts at a different row, the
Row(WordList)
part will need to be adjusted accordingly. 如果从另一行开始,则需要相应地调整
Row(WordList)
部分。
If you want a case-sensitive match, replace SEARCH with FIND in the above formulas 如果要区分大小写 ,请在上述公式中将SEARCH替换为FIND
This is the results, using the above formulas: 使用上面的公式,这是结果:
Is there a limit to the # of words to look up? 查找的单词数是否有限制? If so, a solution (ugly, but avoids VBA) is to use hidden columns on the right to check if each word from Sheet2 is found, and B1 would simply add up those that are.
如果是这样,一种解决方案(难看,但避免使用VBA)是使用右侧的隐藏列来检查是否找到Sheet2中的每个单词,而B1会简单地将它们相加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.