简体   繁体   English

计算单元格中范围内的单词数

[英]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)易于安装和使用:

  1. ALT-F11 brings up the VBE window ALT-F11弹出VBE窗口
  2. ALT-I ALT-M opens a fresh module ALT-I ALT-M打开一个新模块
  3. paste the stuff in and close the VBE window 将内容粘贴并关闭VBE窗口

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:

  1. bring up the VBE window as above 如上调出VBE窗口
  2. clear the code out 清除代码
  3. close the VBE window 关闭VBE窗口

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解决方案,请尝试以下操作:

  • Phrases are in Column A 短语在A列中
  • WordList is a 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. WordList是一个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. 然后选择刚填充的单元格,并根据需要向下填充。

Notes 笔记

  • 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.

相关问题 计算excel单元格范围内超过2个字符的单词数 - Count number of words more than 2 characters in a cell range in excel Excel:对于一个单元格,用一字计算一列中使用的字数 - Excel: For a cell, count the number of words used from a column with words Python-Excel-使用两个CSV字典计算单元格中的单词数 - Python - excel - count number of words in cell, using two csv dictionaries 如何计算单元格中比给定数字长的单词? - How can I count words that are longer than a given number in a cell? 如何计算一个值的出现次数和一个范围内相邻单元格的值 - How to count number of occurrences of a value and the value of adjacent cell in a range 计算与其他单元格内容匹配的范围内的单元格数 - Count number of-cells in a range that match the content of a different cell 计算与其他单元格内容匹配的范围内的单元格数 - Count number of cells in a range that match the content of a different cell 根据另一个单元格范围计算元素的唯一数量 - Count unique number of elements based on another cell range 计算匹配列和行标题的单元格区域中字符串的出现次数 - Count number of occurrences of a string in a cell range matching column and row headers 计算任意单元格范围内每种填充颜色的出现次数? - Count the number of occurrences of each fill color over an arbitrary range of cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM