对关键字sorting的多行string中出现的字符进行计数

VBA新手。 最后遇到了Excel无法用公式解决的问题。 这是我的样本数据:

----------------------- | | A | B* | ----------------------- | 1 | Text1 | 3001 | ----------------------- | 2 | Text2 | 0231 | ----------------------- | 3 | Text1 | 1003 | ----------------------- | 4 | Text3 | 0012 | ----------------------- 

我想实现f(A1:A4,“Text1”,B1:B4,“3”)= 2。

即我想要计算包含“Text1”的每一行B列中字符“3”的累积出现次数。 请注意,列B中的单元格是text和inttypes的。 如果有一种方法来实现这个没有vba,我也欢迎。

这是我到目前为止:

 Function CountCharInStr(FilterStr As String, FilterRange As Range, SearchStr As String, StrRange As Range) As Integer Dim Idx As Integer, IsFound As Boolean CountCharInStr = 0 IsFound = False For Idx = 1 To FilterRange.Rows.Count IsFound = True If FilterRange(Idx) <> FilterStr Then IsFound = False Exit For End If If IsFound Then CountCharInStr = Len(StrRange(Idx)) - Len(Replace(StrRange(Idx), SearchStr, "")) + CountCharInStr Exit For End If Next Idx End Function 

该函数当前返回0,而不是2的初始值。还想知道是否有一个简单的方法来通过用户定义的函数来进行debugging。

非常感谢!

您可以直接在工作表上实现这一点:

  =SUM(IF(A1:A4="Text1",LEN(B1:B4)-LEN(SUBSTITUTE(B1:B4,"3","")),0)) 

请注意,这是一个数组公式 :您必须在编辑时按Ctrl + Shift + Enter(在公式栏中)而不是Enter。

如果可能,总是避免使用VBA。

让我知道你是否需要解释它是如何工作的。