0

让我们举个例子,问题是:“安妮有多少次有任何评论的科学”?(答案:2次)。

    A         B         C       D
1   Name    Gender  Subject Commentary
2   Anne    Female  Maths   
3   Anne    Female  English 
4   Anne    Female  Science whatever...
5   Anne    Female  Maths   
6   Anne    Female  English 
7   Anne    Female  Science blah, blah 
8   Kath    Female  Maths   
9   Kath    Female  English 
10  Kath    Female  Science 
11  Tony    Male    Maths   etc...
12  Tony    Male    English 
13  Tony    Male    Science blah, blah

我使用 COUNTIFS 解决了它:

=COUNTIFS(Sheet1!A1:A13,"Anne",D1:D13,"*")

但现在我需要搜索所有 Anne+Science 的评论,并在结果单元格中检索它们,这样:“随便……等等等等。”

怎么办?
如果没有办法使用公式,如何使用 VBA?

4

2 回答 2

2
Public Function Commentary(rSource As Range, sName As String, sSubject As String) As String

    Dim vaSource As Variant
    Dim i As Long
    Dim lCnt As Long
    Dim aOutput() As String

    vaSource = rSource.Value

    For i = LBound(vaSource, 1) To UBound(vaSource, 1)
        If vaSource(i, 1) Like sName And vaSource(i, 3) Like sSubject Then
            lCnt = lCnt + 1
            ReDim Preserve aOutput(1 To lCnt)
            aOutput(lCnt) = vaSource(i, 4)
        End If
    Next i

    Commentary = Join(aOutput, "|")

End Function

像这样使用

=Commentary(A1:D13,"Anne","Science")
于 2013-10-21T20:58:01.473 回答
1

您可以使用数据透视表并在那里连接结果,如下所示:

SO19503989 示例

编辑

我应该解释一下,如果有很多条目,请=I6&", "&F7在 I7 中使用,复制到适合并选择系列中的最后一个。

于 2013-10-21T20:45:42.570 回答