1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| Sub Comptabiliser()
Dim DerLig As Long, LigDep As Long
Application.ScreenUpdating = False
Columns("S:Z").ClearContents 'Effacements des résultats précédents
Columns("S:Z").UnMerge 'Enlever les précédentes fusions de cellules
DerLig = Range("A" & Rows.Count).End(xlUp).Row
'***********************************************************************************************************************
'on fait un tri pour réunir les mêmes valeurs
Columns("A:R").Select
ActiveWorkbook.Worksheets("Feuil1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Feuil1").Sort.SortFields.Add Key:=Range("A1:A" & DerLig), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Feuil1").Sort.SortFields.Add Key:=Range("D1:D" & DerLig), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Feuil1").Sort.SortFields.Add Key:=Range("N1:N" & DerLig), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Feuil1").Sort.SortFields.Add Key:=Range("O1:O" & DerLig), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Feuil1").Sort
.SetRange Range("A1:R" & DerLig)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'***********************************************************************************************************************
'Sommes des valeurs entre chaque ligne, si A,D sont égales et N différentes
'Traitement colonne O, total en colonne S
LigDep = 1
For a = 1 To DerLig - 1
If Cells(a, "A") = Cells(a + 1, "A") And Cells(a, "D") = Cells(a + 1, "D") And Cells(a, "N") <> Cells(a + 1, "N") And Cells(a, "O") <> Cells(a + 1, "O") Then
Cells(LigDep, "S") = Cells(a, "O") + Cells(a + 1, "O")
LigDep = a + 2
End If
Next a
'Traitement colonne P, total en colonne T
LigDep = 1
For a = 1 To DerLig - 1
If Cells(a, "A") = Cells(a + 1, "A") And Cells(a, "D") = Cells(a + 1, "D") And Cells(a, "N") <> Cells(a + 1, "N") And Cells(a, "P") <> Cells(a + 1, "P") Then
Cells(LigDep, "T") = Cells(a, "P") + Cells(a + 1, "P")
LigDep = a + 2
End If
Next a
'Traitement colonne Q, total en colonne U
LigDep = 1
For a = 1 To DerLig - 1
If Cells(a, "A") = Cells(a + 1, "A") And Cells(a, "D") = Cells(a + 1, "D") And Cells(a, "N") <> Cells(a + 1, "N") And Cells(a, "Q") <> Cells(a + 1, "Q") Then
Cells(LigDep, "U") = Cells(a, "Q") + Cells(a + 1, "Q")
LigDep = a + 2
End If
Next a
'Traitement colonne R, total en colonne V
LigDep = 1
For a = 1 To DerLig - 1
If Cells(a, "A") = Cells(a + 1, "A") And Cells(a, "D") = Cells(a + 1, "D") And Cells(a, "N") <> Cells(a + 1, "N") And Cells(a, "R") <> Cells(a + 1, "R") Then
Cells(LigDep, "V") = Cells(a, "R") + Cells(a + 1, "R")
LigDep = a + 2
End If
Next a
'***********************************************************************************************************************
'Somme en W,X,Y,Z
Cells(1, "W") = Application.WorksheetFunction.Sum(Range(Cells(1, "S"), Cells(DerLig, "S")))
Cells(1, "X") = Application.WorksheetFunction.Sum(Range(Cells(1, "T"), Cells(DerLig, "T")))
Cells(1, "Y") = Application.WorksheetFunction.Sum(Range(Cells(1, "U"), Cells(DerLig, "U")))
Cells(1, "Z") = Application.WorksheetFunction.Sum(Range(Cells(1, "V"), Cells(DerLig, "V")))
'***********************************************************************************************************************
'Fusion des cellules communes pour une meilleure lecture
For j = 19 To 26
DL = DerLig
For i = DerLig To 1 Step -1
If Cells(i, j) <> "" Then
Range(Cells(i, j), Cells(DL, j)).Merge
DL = i - 1
End If
Next i
Next j
With Columns("S:Z")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End Sub |