我有一个包含多行的工作表,每行都有相同的字段。第一列包含组ID。每行可能有不同的组,它们会混淆。可能存在任意数量的不同组ID。我想浏览范围,并为每个Group ID对该行执行操作。
Group ID
A d001
A d002
B d003
A d004
B d005
C d006
B d007
C d008
输出类似于:
Group A:
d001
d002
d004
Group B:
d003
d005
d007
Group C:
d006
d008
这似乎就像一个SQL'WHERE'语句。提前感谢。
答案 0 :(得分:0)
使用此
Sub test()
Dim oCell As Range, i&, LRow&, KeyGR As Variant, KeyIdGr
Dim Group As Object: Set Group = CreateObject("Scripting.Dictionary")
Dim IdGroup As Object: Set IdGroup = CreateObject("Scripting.Dictionary")
i = 1
LRow = Cells(Rows.Count, "A").End(xlUp).Row
For Each oCell In Range("A2:A" & LRow)
If Not Group.exists(oCell.Value) And oCell.Value <> "" Then
Group.Add oCell.Value, i: i = i + 1
End If
Next
For Each oCell In Range("B2:B" & LRow)
If Not Group.exists(oCell.Value) And oCell.Value <> "" Then
IdGroup.Add oCell.Value, oCell.Offset(, -1).Value
End If
Next
i = 1
For Each KeyGR In Group
Cells(i, "C").Value = "Group " & KeyGR & ":"
i = i + 1
For Each KeyIdGr In IdGroup
If IdGroup(KeyIdGr) = KeyGR Then
Cells(i, "C").Value = KeyIdGr
i = i + 1
End If
Next
Next
End Sub
最终结果