Sub MG27Jun27
Dim Rng As Range, Dn As Range, n As Long, Dic As Object, D(0 To 1), Q As Variant
With Sheets("Sheet1")
Set Rng = .Range("B2", .Range("B" & Rows.Count).End(xlUp))
End With
Set Dic = CreateObject("scripting.dictionary")
For Each Dn In Rng
If Not Dic.Exists(Dn.Value) Then
If Dn.Offset(, -1).Value Like "Car*" Then
D(1) = Dn.Offset(, -1).Value
Else
D(0) = Dn.Offset(, -1).Value
End If
Dic.Add Dn.Value, Array(D(0), D(1))
Else
Q = Dic(Dn.Value)
If Dn.Offset(, -1).Value Like "Car*" Then
If InStr(Q(1), Dn.Offset(, -1).Value) = 0 Then
Q(1) = Q(1) & IIf(Q(1) = "", Dn.Offset(, -1).Value, "," & Dn.Offset(, -1).Value)
End If
Else
If InStr(Q(0), Dn.Offset(, -1).Value) = 0 Then
Q(0) = Q(0) & IIf(Q(0) = "", Dn.Offset(, -1).Value, "," & Dn.Offset(, -1).Value)
End If
End If
Dic(Dn.Value) = Q
End If
Next
Dim Num1 As Long, Num2 As Long, P As String, K As Variant, Sp1 As Variant, c As Long, Sp2 As Variant
With Sheets("Sheet2")
.Cells(1, 1) = "Item": .Cells(1, 2) = "Cust #"
c = 1
Dim ans As Long
For Each K In Dic.keys
[h1] = Dic(K)(1)
If Not IsEmpty(Dic(K)(1)) Then
ans = MsgBox("Item = " & K & vbLf & "Car = ""Yes""" & vbLf & "Random item = ""No""", vbYesNo + vbInformation)
c = c + 1
.Cells(c, 2) = K
Sp1 = Split(Dic(K)(0), ",")
Sp2 = Split(Dic(K)(1), ",")
Num1 = Application.RandBetween(0, UBound(Sp1))
Num2 = Application.RandBetween(0, UBound(Sp2))
.Cells(c, 1) = IIf(ans = vbYes, Sp2(Num2), Sp1(Num1))
Else
Sp1 = Split(Dic(K)(0), ",")
Num1 = Application.RandBetween(0, UBound(Sp1))
c = c + 1
.Cells(c, 2) = K
.Cells(c, 1) = Sp1(Num1)
End If
Next K
End With
End Sub
Regards Mick
Bookmarks