且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果行中有匹配项,则返回列名,以查找多个匹配项

更新时间:2021-10-24 22:46:49

如果您已订阅Office 365 Excel,则可以使用以下数组公式:

If you have a subscription to Office 365 Excel then you can use the following array formula:

=TEXTJOIN(", ",TRUE,IF(($E$2:$H$6 = "X")*($D$2:$D$6=A2),$E$1:$H$1,""))

作为数组公式,退出编辑模式时,需要使用Ctrl-Shift-Enter而不是Enter进行确认.如果操作正确,则Excel会将{}放在公式周围.

Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.

如果您没有Office 365,则可以将此代码放在工作簿附带的模块中,并使用如上所述的公式:

If you do not have Office 365 then you can put this code in a module attached to the workbook and use the formula as described above:

Function TEXTJOIN(delim As String, skipblank As Boolean, arr)
    Dim d As Long
    Dim c As Long
    Dim arr2()
    Dim t As Long, y As Long
    t = -1
    y = -1
    If TypeName(arr) = "Range" Then
        arr2 = arr.Value
    Else
        arr2 = arr
    End If
    On Error Resume Next
    t = UBound(arr2, 2)
    y = UBound(arr2, 1)
    On Error GoTo 0

    If t >= 0 And y >= 0 Then
        For c = LBound(arr2, 1) To UBound(arr2, 1)
            For d = LBound(arr2, 1) To UBound(arr2, 2)
                If arr2(c, d) <> "" Or Not skipblank Then
                    TEXTJOIN = TEXTJOIN & arr2(c, d) & delim
                End If
            Next d
        Next c
    Else
        For c = LBound(arr2) To UBound(arr2)
            If arr2(c) <> "" Or Not skipblank Then
                TEXTJOIN = TEXTJOIN & arr2(c) & delim
            End If
        Next c
    End If
    TEXTJOIN = Left(TEXTJOIN, Len(TEXTJOIN) - Len(delim))
End Function