如何通过匹配/索引从数据矩阵中获取多个唯一结果

如何通过匹配/索引从数据矩阵中获取多个唯一结果

我有两列:

    Column A     Column B

 1  Course ID   Professors  <---- (Column Headers)  
 2  1179/03     Professor-1  
 3  1179/03     
 4  1179/03     
 5  1300/20     Professor-2   
 6  1300/20     Professor-3  
 7  1300/21     Professor-2  
 8  1300/21     Professor-3  
 9  1300/21     Professor-4  
10  1300/21     Professor-4
11  1300/21     Professor-4

其中,每个课程 ID 可以分配多位教授。

在同一张表的另一部分,我有一个课程 ID 列表,我想通过公式插入指定的教授:

    Column D     Column E

1  Course ID   Professors   <---  (Column Header)  
2  1179/03     Professor-1  
3  1300/20     Professor-2, Professor-3  
4  1300/21     Professor-2, Professor-3, Professor-4

我将在A列,那么我希望得到结果E2E3E4通过Match/IndexVLOOKUP
我的限制是,我无法添加任何新列,并且可能限制用户根据某一列对数据进行排序。

有人能帮帮我吗?

答案1

这个简短的宏包含与您发布的数据类似的数据:

Sub Roster()
    Dim rc As Long, i As Long, j As Long, v As String
    Dim nA As Long, nB As Long, nD As Long, vv As String

    rc = Rows.Count
    nA = Cells(rc, 1).End(xlUp).Row
    nB = Cells(rc, 2).End(xlUp).Row
    nD = Cells(rc, 4).End(xlUp).Row

    For i = 2 To nD
        v = Cells(i, 4)
        vv = ""
        For j = 2 To nA
            If v = Cells(j, 1) And Cells(j, 2) <> "" And InStr(1, vv, Cells(j, 2)) = 0 Then
                vv = vv & "," & Cells(j, 2)
            End If
        Next j
        Cells(i, 5) = Mid(vv, 2)
    Next i
End Sub

将产生:

在此处输入图片描述

相关内容