Sub Delta()
Dim i As Long, j As Long, k As Long, v8, v9
Dim a8(), a9(), d89(), UL8 As Long, UL9 As Long, UL89 As Long, UC8 As Long, UC9 As Long
Dim r2Calc As Long
r2Calc = Application.Calculation
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
a8 = Sheets("2008").Cells(1, 1).CurrentRegion.Value
a9 = Sheets("2009").Cells(1, 1).CurrentRegion.Value
d89 = Sheets("ECART").Range(Cells(1, 1), Cells(1, 5)).Value
d89 = Application.Transpose(d89)
UL8 = UBound(a8, 1)
UL9 = UBound(a9, 1)
UC8 = UBound(a8, 2)
UC9 = UBound(a9, 2)
UL89 = 1
Sheets.Add Before:=Sheets("ECART")
With ActiveSheet
.Columns("A:A").NumberFormat = "@"
With .Range(.Cells(1, 1), .Cells(UL8 + 1, UC8))
.Value = a8
.Cells(UL8 + 1, 1).Value = Chr(255)
.Sort key1:=.Cells(1, 1), order1:=xlAscending, header:=xlYes, MatchCase:=True
a8 = .Value
End With
.Cells.ClearContents
With .Range(.Cells(1, 1), .Cells(UL9 + 1, UC9))
.Value = a9
.Cells(UL9 + 1, 1).Value = Chr(255)
.Sort key1:=.Cells(1, 1), order1:=xlAscending, header:=xlYes, MatchCase:=True
a9 = .Value
End With
Application.DisplayAlerts = False
Application.EnableEvents = False
.Delete
Application.EnableEvents = True
Application.DisplayAlerts = True
End With
i = 2
j = 2
Do Until i + j > UL8 + UL9 + 1
UL89 = UL89 + 1
ReDim Preserve d89(1 To 5, 1 To UL89)
v8 = a8(i, 1)
v9 = a9(j, 1)
If v9 = v8 Then
For k = 1 To 4: d89(k, UL89) = a9(j, k): Next k
d89(3, UL89) = d89(3, UL89) - a8(i, 3)
i = i + 1: j = j + 1
Else
If v9 > v8 Then
For k = 1 To 4: d89(k, UL89) = a8(i, k): Next k
d89(3, UL89) = -a8(i, 3)
d89(5, UL89) = 2008
i = i + 1
Else
For k = 1 To 4: d89(k, UL89) = a9(j, k): Next k
d89(5, UL89) = 2009
j = j + 1
End If
End If
Loop
Sheets("ECART").Cells(1, 1).CurrentRegion.ClearContents
Sheets("ECART").Range(Cells(1, 1), Cells(UL89, 5)).Value = Application.Transpose(d89)
Application.ScreenUpdating = True
Application.Calculation = r2Calc
End Sub