Excel Vlookup或公式

问题描述

有人可以在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

在另一个工作表2中,我具有所有唯一的用户名,希望在其中列出工作表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

解决方法

在工作表2-

中使用以下公式
=IFERROR(INDEX(Sheet1!$B$1:$B$7,AGGREGATE(15,6,ROW(Sheet1!$A$1:$A$7)/(Sheet1!$A$1:$A$7=$A1),COLUMN(A$1))),"")

如果您有Office365,则可以通过Transpose()Filter()公式来简化它-

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

enter image description here

,

我不确定公式是否可以实现此功能,但是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