السلام عليكم
تم استخدام الكود التالي
Option Explicit
Private Const ContColmn As Integer = 3
'======================================================
'======================================================
Sub kh_Report()
Dim obj As Object
Dim x(), AryList()
Dim iKey As String
Dim iTm As Range, Rng As Range
Dim LastRow As Long, iCont As Long
Dim i As Long, ii As Long, iii As Long
Dim c As Integer
Dim v1 As Double, v2 As Double
'============================================
Set obj = CreateObject("Scripting.Dictionary")
'============================================
With Cells.Worksheet
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
With .Range("B4")
.Activate
.Resize(1, ContColmn).ClearContents
.Offset(1, 0).Resize(LastRow, ContColmn).Clear
End With
End With
'============================================
With ورقة2
LastRow = .Cells(Rows.Count, "C").End(xlUp).Row
Set Rng = .Range("C3:C" & LastRow)
End With
'============================================
On Error GoTo kh_Ex
'============================================
For Each iTm In Rng
If CStr(iTm.Cells(1, 3)) = CStr(Range("C2")) Then
iKey = iTm.Value
v1 = Val(iTm.Cells(1, 4))
v2 = Val(iTm.Cells(1, 2))
'''''''''''''''''''
If obj.exists(iKey) Then
iii = obj(iKey)
''''''''''''''''''
x(2, iii) = Val(x(2, iii)) + v1
x(3, iii) = Val(x(3, iii)) + v2
Else
ii = ii + 1
ReDim Preserve x(1 To ContColmn, 1 To ii)
obj.Add iKey, ii
''''''''''''''''''
x(1, ii) = iKey
x(2, ii) = v1
x(3, ii) = v2
End If
End If
Next
'============================================
iCont = obj.Count
If iCont Then
ReDim AryList(1 To iCont, 1 To ContColmn)
For i = 1 To iCont
''''''''''''''''''
For c = 1 To 3
AryList(i, c) = x(c, i)
Next
''''''''''''''''''
Next
'============================================
With Range("B4").Resize(iCont, ContColmn)
If iCont > 1 Then .Rows(1).AutoFill .Cells, xlFillFormats
.Value = AryList
End With
'''''''''''''''''''''''''
End If
'============================================
kh_Ex:
''''''''''''''''''
If Err Then
MsgBox "Err.Number : " & Err.Number
Err.Clear
Else
If iCont Then MsgBox "تم تحديث التقرير بنجاح ", vbMsgBoxRight, "الحمدلله"
End If
''''''''''''''''''
Set obj = Nothing
Set Rng = Nothing
Erase x, AryList
''''''''''''''''''
End Sub
شاهد المرفق 2003
تصفية وتجميع.rar