Excel中错综复杂的字数统计

我需要在电子表格中给出一个特定的名字来findAVERAGE WORD COUNT。 一个例子是:

作者词

Person1这是一个例子

Person1也是这样

Person1也是这样

Person2这些例子吸引

Person2这个更好…

我有一个函数来计算这个单词:

Function intWordCount(rng As Range) As Integer intWordCount = UBound(Split(rng.Value, " "), 1) + 1 End Function 

我对如何通过给定一个名字(PersonX)循环列表感到困惑,将这些单词的总数相加,然后除以每个人的总数。

希望这是明确的…

谢谢,Stu

第一步是改变你的代码来处理多个单元格; 我去的方式是用For Each循环遍历范围中的每一行。

 For Each Row In rng.Rows ... Next 

下一步是检查是否需要将第一列与某个值str比较的行。

 For Each Row In rng.Rows If (Row.Cells(1, 1).Value = str) Then ... End If Next 

把这两块与你原来的function放在一起,你会得到这样的function。

 Function avgWordCount(rng As Range, str As String) As Double Dim counter As Long Dim wordCount As Long Dim Row As Range For Each Row In rng.Rows If (Row.Cells(1, 1).Value = str) Then counter = counter + 1 wordCount = wordCount + UBound(Split(Row.Cells(1, 2).Value, " "), 1) + 1 End If Next avgWordCount = wordCount / counter End Function 

它可以使用单元格引用或静态string进行调用。

结果