السلام عليكم
هذا الكود مجرب على قاعدة بيانات
فيها اكثر من 15000 سجل
Option Explicit
Private Const ContColmn As Integer = 5
'======================================================
'======================================================
Sub kh_mReport()
Dim xx
Dim x(), AryList()
Dim Rng As Range
Dim i As Long, LastRow As Long, iCont As Long
Dim c As Integer, m As Integer
Dim Md As Double, Dn As Double
Dim v1 As Double, v2 As Double
Dim S As String
'''''''''''''''''''''
Dim Co As New Collection
'============================================
With Cells.Worksheet
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
With .Range("A2")
.Activate
.Resize(1, ContColmn).ClearContents
.Offset(1, 0).Resize(LastRow, ContColmn).Clear
End With
End With
'============================================
With Sheets("dailyd1ary")
LastRow = .Cells(Rows.Count, "C").End(xlUp).Row
Set Rng = .Range("C2:G" & LastRow)
End With
'============================================
On Error GoTo kh_ex
kh_Application False
'''''''''''''''''''''
ReDim x(0 To 2)
With Rng
For i = 1 To .Rows.Count
v1 = 0: v2 = 0
1: On Error Resume Next
''''''''''''''''''
Md = Val(.Cells(i, 2))
Dn = Val(.Cells(i, 3))
S = CStr(.Cells(i, 4))
''''''''''''''''''
x(0) = Val(S)
x(1) = Md + v1
x(2) = Dn + v2
'''''''''''''''''''
Co.Add x, S
'''''''''''''''''''
If Err Then
v1 = Val(Co(S)(1)): v2 = Val(Co(S)(2))
Co.Remove S
Err.Clear
GoTo 1
End If
'''''''''''''''''''
Next
End With
'============================================
iCont = Co.Count
If iCont Then
Set Rng = Sheets("accounts").Range("A2:A1000")
ReDim AryList(1 To iCont, 1 To ContColmn)
For i = 1 To iCont
xx = Co.Item(i)
On Error Resume Next
m = WorksheetFunction.Match(xx(0), Rng, 0)
If Err Then m = 0: Err.Clear
AryList(i, 1) = xx(0)
If m Then AryList(i, 2) = Rng.Cells(m, 2)
AryList(i, 3) = xx(1)
AryList(i, 4) = xx(2)
AryList(i, 5) = Val(xx(1)) - Val(xx(2))
Next
'''''''''''''''''''''''''
With Range("A2").Resize(iCont, ContColmn)
If iCont > 1 Then .Rows(1).AutoFill .Cells, xlFillFormats
.Value = AryList
.Sort .Columns(1), xlAscending
End With
'''''''''''''''''''''''''
End If
'============================================
kh_ex:
kh_Application True
''''''''''''''''''
If Err Then
MsgBox "Err.Number : " & Err.Number
Err.Clear
Else
MsgBox "تم تحديث الميزان بنجاح ", vbMsgBoxRight, "الحمدلله"
End If
Set Co = Nothing
Set Rng = Nothing
Erase AryList, x
End Sub
Sub kh_Application(mbol As Boolean)
With Application
.Calculation = IIf(mbol, -4105, -4135)
.ScreenUpdating = mbol
.EnableEvents = mbol
End With
End Sub
المرفق 2003-2007
ميزان مراجعة.rar