STPM string array is the concatenated keys of the columns
Sales Person, Team and Model;
I guess I need to sort Column D and E also to get the correct results.
Kindly correct me if I am wrong.
Sub EasyDemo1()
Const DL = ", "
Application.ScreenUpdating = False: L& = 2
With Cells(7).CurrentRegion.Rows
If .Count > 2 Then .Item("3:" & .Count).Clear
End With
With Cells(1).CurrentRegion.Rows
For R& = 3 To .Count
With .Item(R)
If .Cells(1).Value = Cells(L, 7).Value And .Cells(2).Value = Cells(L, 8).Value And .Cells(3).Value = Cells(L, 9).Value Then
For C% = 4 To 5
If InStr(DL & Cells(L, C + 6).Value & DL, DL & .Cells(C).Value & DL) = 0 Then Cells(L, C + 6).Value = Cells(L, C + 6).Value & DL & .Cells(C).Value
Next
Else
L = L + 1
.Cells.Copy Rows(L).Columns("G:K")
End If
End With
Next
End With
With Cells(7).CurrentRegion.Columns("D:E"): .HorizontalAlignment = xlCenter: .AutoFit: End With
End Sub
Sub EasyDemo2()
Const DL = ", "
Application.ScreenUpdating = False: L& = 2
With Cells(1).CurrentRegion.Rows
If .Count > 2 Then .Item("3:" & .Count).Clear
End With
With Sheet1.Cells(1).CurrentRegion.Rows
For R& = 3 To .Count
With .Item(R)
If .Cells(1).Value = Cells(L, 1).Value And .Cells(2).Value = Cells(L, 2).Value And .Cells(3).Value = Cells(L, 3).Value Then
For C% = 4 To 5
If InStr(DL & Cells(L, C).Value & DL, DL & .Cells(C).Value & DL) = 0 Then Cells(L, C).Value = Cells(L, C).Value & DL & .Cells(C).Value
Next
Else
L = L + 1
Rows(L).Columns("A:E").Value = .Cells.Value
End If
End With
Next
End With
With Cells(1).CurrentRegion.Columns("D:E"): .HorizontalAlignment = xlCenter: .AutoFit: End With
Application.Goto Cells(1), True
End Sub
Waiting for the Sir !I have not yet shown you hybrid way
Sub DemoH1()
Application.ScreenUpdating = False
With Cells(1).CurrentRegion.Rows
If .Count > 2 Then .Item("3:" & .Count).Clear
End With
With Sheet1.Cells(1).CurrentRegion.Rows
If .Count < 3 Then Exit Sub
.Item("2:" & .Count).Columns("A:C").AdvancedFilter xlFilterCopy, , [A2:C2], True
With Cells(1).CurrentRegion.Rows
With .Item("3:" & .Count).Columns("A:C").Rows
.Borders.LineStyle = xlLineStyleNone: .Font.Size = 11
ReDim SPTM$(1 To .Count), OUTP$(1 To .Count, 4 To 5)
For L& = 1 To .Count: SPTM(L) = .Cells(L, 1) & "¤" & .Cells(L, 2) & "¤" & .Cells(L, 3): Next
End With
End With
D = Array("", ", ")
For R& = 3 To .Count
L = Application.Match(.Cells(R, 1) & "¤" & .Cells(R, 2) & "¤" & .Cells(R, 3), SPTM, 0)
For C% = 4 To 5
If InStr(D(1) & OUTP(L, C) & D(1), D(1) & .Cells(R, C).Value & D(1)) = 0 Then _
OUTP(L, C) = OUTP(L, C) & D(-(OUTP(L, C) > "")) & .Cells(R, C).Value
Next
Next
End With
[D3:E3].Resize(UBound(OUTP)).Value = OUTP
With Cells(1).CurrentRegion.Columns("D:E"): .HorizontalAlignment = xlCenter: .AutoFit: End With
Application.Goto Cells(1), True
End Sub
Sub DemoH2()
Application.ScreenUpdating = False
With Cells(1).CurrentRegion.Rows
If .Count > 2 Then .Item("3:" & .Count).Clear
End With
With Sheet1.Cells(1).CurrentRegion.Rows
If .Count < 3 Then Exit Sub
.Item("2:" & .Count).Columns("A:C").AdvancedFilter xlFilterCopy, , [A2:C2], True
With Cells(1).CurrentRegion.Rows
With .Item("3:" & .Count).Columns("A:C").Rows
.Borders.LineStyle = xlLineStyleNone: .Font.Size = 11
ReDim SPTM$(1 To .Count), OUTP$(1 To .Count, 4 To 5)
For L& = 1 To .Count: SPTM(L) = Join$(Application.Transpose(Application.Transpose(.Item(L).Value)), "¤"): Next
' For L& = 1 To .Count: SPTM(L) = Join$(Application.Index(.Item(L).Resize(2).Value, 1), "¤"): Next
End With
End With
D = Array("", ", ")
For R& = 3 To .Count
L = Application.Match(Join(Application.Transpose(Application.Transpose(.Item(R).Columns("A:C").Value)), "¤"), SPTM, 0)
' L = Application.Match(Join(Application.Index(.Item(R).Resize(2).Columns("A:C").Value, 1), "¤"), SPTM, 0)
For C% = 4 To 5
If InStr(D(1) & OUTP(L, C) & D(1), D(1) & .Cells(R, C).Value & D(1)) = 0 Then _
OUTP(L, C) = OUTP(L, C) & D(-(OUTP(L, C) > "")) & .Cells(R, C).Value
Next
Next
End With
[D3:E3].Resize(UBound(OUTP)).Value = OUTP
With Cells(1).CurrentRegion.Columns("D:E"): .HorizontalAlignment = xlCenter: .AutoFit: End With
Application.Goto Cells(1), True
End Sub
Sub DemoDict1()
Const DL = ", "
Dim Dict As Object, SP$()
With Cells(1).CurrentRegion.Rows
If .Count > 2 Then .Item("3:" & .Count).Clear
End With
With Sheet1.Cells(1).CurrentRegion.Rows
If .Count < 3 Then Beep: Exit Sub
Application.ScreenUpdating = False
Set Dict = CreateObject("Scripting.Dictionary")
For R& = 3 To .Count
With .Item(R)
K$ = .Cells(1).Value & vbTab & .Cells(2).Value & vbTab & .Cells(3).Value
SP = Split(Dict.Item(K), vbTab)
If UBound(SP) > 0 Then
For C% = 0 To 1
If InStr(DL & SP(C) & DL, DL & .Cells(4 + C).Value & DL) = 0 Then _
SP(C) = SP(C) & DL & .Cells(4 + C).Value
Next
Dict.Item(K) = Join$(SP, vbTab)
Else
Dict.Item(K) = .Cells(4).Value & vbTab & .Cells(5).Value
End If
End With
Next
End With
With [A3].Resize(Dict.Count)
.Value = Application.Transpose(Dict.Keys): .TextToColumns DataType:=xlDelimited, Tab:=True
End With
With [D3].Resize(Dict.Count)
.Value = Application.Transpose(Dict.Items): .TextToColumns DataType:=xlDelimited, Tab:=True
End With
With [D2:E2].Resize(Dict.Count + 1): .HorizontalAlignment = xlCenter: .Columns.AutoFit: End With
Dict.RemoveAll: Set Dict = Nothing
Application.Goto Cells(1), True
End Sub
Sub DemoDict2()
Const DL = ", "
With Sheet1.Cells(1).CurrentRegion.Rows
If .Count < 3 Then Beep: Exit Sub
VA = .Item("3:" & .Count).Value
End With
With Cells(1).CurrentRegion.Rows
Application.ScreenUpdating = False
If .Count > 2 Then .Item("3:" & .Count).Clear
End With
With CreateObject("Scripting.Dictionary")
For R& = 1 To UBound(VA)
K$ = VA(R, 1) & vbTab & VA(R, 2) & vbTab & VA(R, 3)
SP = Split(.Item(K), vbTab)
If UBound(SP) > 0 Then
For C% = 0 To 1
T$ = VA(R, 4 + C)
If IsError(Application.Match(T, Split(SP(C), DL), 0)) Then SP(C) = SP(C) & DL & T
Next
.Item(K) = Join$(SP, vbTab)
Else
.Item(K) = VA(R, 4) & vbTab & VA(R, 5)
End If
Next
[A3].Resize(.Count).Value = Application.Transpose(.Keys)
[D3].Resize(.Count).Value = Application.Transpose(.Items)
.RemoveAll
End With
With Cells(1).CurrentRegion.Rows
With .Item("3:" & .Count)
For Each VA In [{1,4}]: .Columns(VA).TextToColumns , xlDelimited, , , True: Next
End With
With .Columns("D:E"): .HorizontalAlignment = xlCenter: .AutoFit: End With
Application.Goto .Cells(1), True
End With
End Sub