نعم اخي @hanykassem نظرا للمثال المرفق هناك بعض الإحتمالات الواردة في حالة كان هناك تكرار لنفس القيم كما هو موضح في الصورة أدناه
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WS As Worksheet: Set WS = Sheets("Sheet1")
Dim i As Long, ling As Long, lastRow As Long, tmp As String, kayB As String, kayC As String, _
j As Variant, a As Object, r As Object
Set a = CreateObject("Scripting.Dictionary"): Set r = CreateObject("Scripting.Dictionary")
If Not Intersect(Target, WS.Range("A4:C" & WS.Rows.Count)) Is Nothing Then
Application.ScreenUpdating = False
With WS
.Range("I3:K" & .Rows.Count).ClearContents
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
ling = 3
For i = 4 To lastRow
tmp = .Cells(i, 1).value
kayB = .Cells(i, 2).value
kayC = .Cells(i, 3).value
If tmp <> "" Then
If kayB <> "" Then a(tmp) = IIf(a.Exists(tmp), a(tmp) & " , " & kayB, kayB)
If kayC <> "" Then r(tmp) = IIf(r.Exists(tmp), r(tmp) & " , " & kayC, kayC)
End If
Next i
For Each j In a.Keys
.Cells(ling, 9).value = j
.Cells(ling, 10).value = a(j)
.Cells(ling, 11).value = r(j)
ling = ling + 1
Next j
.Columns("j:K").AutoFit
End With
Application.ScreenUpdating = True
End If
End Sub
لحدف التكرارات قم بتعديل الصف التالي
If tmp <> "" Then
If kayB <> "" Then a(tmp) = IIf(a.Exists(tmp), a(tmp) & " , " & kayB, kayB)
If kayC <> "" Then r(tmp) = IIf(r.Exists(tmp), r(tmp) & " , " & kayC, kayC)
End If
إلى
If tmp <> "" Then
If kayB <> "" Then If Not a.Exists(tmp) Then a.Add tmp, _
kayB Else If InStr(1, a(tmp), kayB) = 0 Then a(tmp) = a(tmp) & " , " & kayB
If kayC <> "" Then If Not r.Exists(tmp) Then r.Add tmp, _
kayC Else If InStr(1, r(tmp), kayC) = 0 Then r(tmp) = r(tmp) & " , " & kayC
End If
TEST CODE 2.xlsb