[英]Excel: For a cell, count the number of words used from a column with words
Goal: search whether words from a column (with a list or words) exist in a cell with a sentence 目标:搜索带有句子的单元格中是否存在列中的单词(带有列表或单词)
Explanation : I have two columns, say A contains one sentence per cell. 说明 :我有两列,说A每个单元格包含一个句子。 Column B contains a list with one word per cell.
B列包含一个列表,每个单元格包含一个单词。 For each cell in column A, I want to know whether a word is used from the list in column B.
对于A列中的每个单元格,我想知道是否使用了B列中的单词。
I would like 2 different outputs: 我想要2种不同的输出:
1) In column C, return the number of words used from column B in the sentence in the cell in column A. Say, in cell C2, return the number of words used from column B in the sentence in cell A2. 1)在C列中,返回在A列的单元格中从句子的B列使用的单词数。说,在C2单元格中,返回在A2单元中的句子中B列的使用的单词数。
For example, if the word list in column B contains: 例如,如果B列中的单词列表包含:
B2: monkey B2:猴子
B3: donkey B3:驴
B4: giraffe B4:长颈鹿
B5: elephant B5:大象
And the sentence in cell A2 says: "the monkey and the elephant are walking" then I would like to return the number "2" in cell C2. A2单元格中的句子说:“猴子和大象在走路”,那么我想在C2单元中返回数字“ 2”。
2) In column D until Z? 2)在D列中直到Z? I would like to return the words that are used.
我想返回所使用的词。 So in case of the above example I would like to return in cell:
因此,在上述示例中,我想在单元格中返回:
D2: monkey D2:猴子
E2: elephant E2:大象
Hopefully somebody can help me out! 希望有人可以帮助我!
This is for the first part: 这是第一部分:
Try this small UDF() : 试试这个小的UDF() :
Public Function WordCount(r1 As Range, r2 As Range) As Long
ary = Split(r1.Text, " ")
bry = r2.Value
For Each a In ary
For Each b In bry
If a = b Then WordCount = WordCount + 1
Next b
Next a
End Function
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:
=myfunction(A1) = myfunction(A1)
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! 必须启用宏才能使其正常工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.