Excel Vlookup 或公式

Excel Vlookup or formula

有人可以为以下内容推荐 Excel 中的公式吗?

我在工作表 1 中有以下数据,其中用户分配有多个角色,用户有多个角色,每个角色都列在单独的行中

A----------B
User 1 - Role 1
User 1 - Role 2
User 1 - Role 5
User 2 - Role 2
User 2 - Role 5
User 3 - Role 1
User 3 - Role 3

在另一个 sheet2 中,我有所有唯一的用户名,我希望在其中列出来自 Sheet 1 的数据

A----------B-------C---------D
User 1 - Role 1 - Role 2 - Role 5
User 2 - Role 2 - Role 5
User 3 - Role 1 - Role 3

我不确定公式是否可行,但VBA会按如下方式进行

Dim Lrow as Long
Lrow = shtFrom.Range("A1").End(xlUp).Row
Dim RoleCount as Long
Dim shtFrom as Worksheet, shtTo as Worksheet

Set shtFrom = Sheets("Sheet1")
Set shtTo = Sheets("Sheet2")

For x = 1 to shtTo.Range("A1").End(xlUp).Row
    RoleCount = 0
    For y = 1 to Lrow
        If shtFrom.Cells(y, 1).Value Like shtTo.Cells(x, 1).Value Then
            RoleCount = RoleCount + 1
            shtTo.Cells(y, RoleCount + 1).Value = shtTo.Cells(x, 2).Value
        End If
    Next y
Next x

在sheet2中使用下面的公式-

=IFERROR(INDEX(Sheet1!$B:$B,AGGREGATE(15,6,ROW(Sheet1!$A:$A)/(Sheet1!$A:$A=$A1),COLUMN(A))),"")

如果你有Office365那么你可以用Transpose()Filter()公式简化它-

=TRANSPOSE(FILTER(Sheet1!$B:$B,Sheet1!$A:$A=A1))