
我有兩列:
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,乙和D列,然後我想得到結果E2,E3,E4通過Match/Index
或 的方式VLOOKUP
。
我的限制是,我無法新增任何新列,並且可能會限制使用者根據特定列對資料進行排序。
請任何人幫助我。
答案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
將產生: