我在Excel中有一个看起来像这样的表
role user
a Joe
b Joe
c Joe
a Frank
第二张表
role
a
b
c
是否可以像示例一样列出每个用户的缺失角色?
Frank b c
答案 0 :(得分:1)
哇。这比预期的要困难得多。不是最优雅的解决方案。需要先排序user
列,以便所有用户在一起。
如果role
超过一个单词XD
Sub wert()
Dim roles
Dim lastRow As Long
Dim fRow As Long
Dim user As String
Dim except As String
Dim list As String
user = Application.InputBox("which user?") 'prompt for user
With ActiveSheet
lastRow = .Cells(.Rows.count, "B").End(xlUp).Row
roles = .Range("D2:D4") 'change range as needed
With .Range("B2:B" & lastRow)
Set f = .Find(user, LookIn:=xlValues, lookat:=xlWhole) 'search for user
End With
If Not f Is Nothing Then fRow = f.Row 'if found
list = .Cells(fRow, 1)
For i = fRow To lastRow
If .Cells(i + 1, 2) = .Cells(i, 2) Or .Cells(i - 1, 2) = .Cells(i, 2) Then
list = list & .Cells(i, 1) 'add role to list
'Debug.Print list
Else
Exit For
End If
Next
For Each e In roles
If InStr(list, e) > 0 Then
Else
except = except & " " & e 'if role not found in list, then add it to string
End If
Next
.Range("F1").Value = user & ": " & except 'change range if desired
End With
End Sub