Option Explicit
Sub Retraitement2()
Dim a, b, CorresParam, i As Long, ii As Long, iii As Long, n As Long, nCompte, libelCompte, EnTete
a = Sheets("Grand-livre des comptes").Range("A1").CurrentRegion.Value
CorresParam = Sheets("Paramètres_comptes").Range("A1").CurrentRegion.Value
EnTete = [{"N° du Compte","Libellé du Compte","Date","Année","Mois","Libellé","Débit","Crédit","Solde","Outils analytiques","Agregat","Agregat final"}]
ReDim b(1 To UBound(a, 1), 1 To 12)
For i = 2 To UBound(a, 1)
ii = 0
Do While Year(a(i + ii, 2)) = 2024 Or Year(a(i + ii, 2)) = 2023
If ii = 0 Then
nCompte = a(i - 1 + ii, 1)
libelCompte = a(i - 1 + ii, 4)
End If
b(n + 1, 1) = nCompte ' n°Compte
b(n + 1, 2) = libelCompte ' Libellé du Compte
If IsDate(a(i + ii, 1)) Then
b(n + 1, 3) = a(i + ii, 1) ' Date
End If
b(n + 1, 4) = Year(a(i + ii, 2)) ' Annee
b(n + 1, 5) = MonthName(Month(a(i + ii, 3))) ' Mois
b(n + 1, 6) = a(i + ii, 4) ' Libellé Ecritures
b(n + 1, 7) = a(i + ii, 5) ' Débit
b(n + 1, 8) = a(i + ii, 6) ' Crédit
b(n + 1, 9) = b(n + 1, 7) - b(n + 1, 8) 'Solde
For iii = 2 To UBound(CorresParam, 1)
If CorresParam(iii, 1) = nCompte Then
b(n + 1, 10) = CorresParam(iii, 2) ' Outils Analytiques
b(n + 1, 11) = CorresParam(iii, 3) ' Agregat
b(n + 1, 12) = CorresParam(iii, 4) ' Agregat Final
Exit For
End If
Next
ii = ii + 1: n = n + 1
' Sort de la boucle si fin de colonne
If i + ii > UBound(a, 1) Then Exit Do
Loop
If ii > 0 Then
i = i + ii - 1
End If
Next
' Restitution
Application.ScreenUpdating = False
If Not Evaluate("isref('Retraitement1'!a1)") Then Sheets.Add(, Sheets(Sheets.Count)).Name = "Retraitement1"
With Sheets("Retraitement1")
With .Cells(1)
.CurrentRegion.Clear
If n > 0 Then
.Resize(, UBound(b, 2)).Value = EnTete
.Offset(1).Resize(n, UBound(b, 2)).Value = b
With .CurrentRegion
.Font.Name = "calibri"
.Font.Size = 10
.VerticalAlignment = xlCenter
.Borders(xlInsideVertical).Weight = xlThin
.BorderAround Weight:=xlThin
With .Rows(1)
.HorizontalAlignment = xlCenter
.Font.Size = 11
.BorderAround Weight:=xlThin
.Interior.ColorIndex = 44
End With
.Columns.AutoFit
End With
End If
End With
End With
Application.ScreenUpdating = True
End Sub