选择匹配单元格旁边的值

时间:2019-03-22 01:46:27

标签: excel

我有一个包含两列的电子表格,keyname。现在,每个值对该名称重复多次。我设置了一个公式来获取名称中的唯一值,但是现在我需要获取与该列匹配的所有单元格值的列表。

因此,如果我的名字是“ Brian”,并且其中有4个的索引值为5、6、7和8,我需要一个公式来采用/使用C1的值来查找在B:B,并给我A:A中的所有值。

这可能吗?

example of data Desired result

2 个答案:

答案 0 :(得分:2)

在您的的D1中,将此公式放入并向下拖动。

=arrayformula(textjoin(" ", true, if(B:B=C1, A:A, "")))

enter image description here

答案 1 :(得分:0)

CONCIF(UDF)

在Excel中

=CONCIF($B$1:$B$20,$C1,$A$1:$A$20)

在VBA中

Option Explicit

Function CONCIF(MatchRange As Range, ByVal MatchValue As Variant, _
        ConcatRange As Range, _
        Optional ByVal Delimiter As String = " ") As String

    Dim vntM As Variant   ' Match Array
    Dim vntC As Variant   ' Concat Array
    Dim Nor As Long       ' Number of Rows
    Dim i As Long         ' Row Counter
    Dim strC As String    ' Concat String
    Dim strR As String    ' Result String

    ' Check number of rows in MatchRange is less than or equal to number
    ' of rows in Concat Range.
    If MatchRange.Rows.Count <= ConcatRange.Rows.Count Then
        ' Write number of rows in MatchRange to Number of Rows.
        Nor = MatchRange.Rows.Count
      Else
        ' Write number of rows in ConcatRange to Number of Rows.
        Nor = ConcatRange.Rows.Count
    End If

    ' Check if Number of Rows is equal to 1, which would mean there
    ' can only be one match.
    If Nor = 1 Then
        ' Check if the value in 1-cell MatchRange is equal to MatchValue.
        If MatchRange.Cells(1, 1) = MatchValue Then
            ' Write value of 1-cell ConcatRange, converted to string, to CONCIF.
            CONCIF = CStr(ConcatRange.Cells(1, 1))
        End If
        Exit Function
    End If

    ' Copy the range defined by 1st Nor number of cells in 1st column of
    ' MatchRange to 2D 1-based 1-column Match Array.
    vntM = MatchRange.Cells(1, 1).Resize(Nor)
    ' Copy the range defined by 1st Nor number of cells in 1st column of
    ' ConcatRange to 2D 1-based 1-column Concat Array.
    vntC = ConcatRange.Cells(1, 1).Resize(Nor)

    ' Loop through elements (rows) of Match/Concat Arrays.
    For i = 1 To Nor
        ' Check if current value in MatchArray is equal to MatchValue.
        If vntM(i, 1) = MatchValue Then
            ' Write current value in ConcatArray to Concat String.
            strC = CStr(vntC(i, 1))
            ' Check if Concat String is NOT "".
            If strC <> "" Then
                ' Check if Result String is NOT "".
                If strR <> "" Then
                    ' Concatenate current value of Result String, Delimiter
                    ' and Concat String, to Result String.
                    strR = strR & Delimiter & strC
                  Else
                    ' Write Concat String to Result String (only once).
                    strR = strC
                End If
            End If
        End If
    Next

    ' Write Result String to CONCIF.
    CONCIF = strR

End Function

enter image description here