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
| Sub Compil()
Dim sh As Variant
Dim shComp As Worksheet
Dim shName As Variant
Dim R As Integer
Dim PVT As PivotTable
'on définit les nom des feuilles contenant les tableaux a compiler
shName = Array("sh1", "sh2", "sh3")
' on ajoute une feuille pour la compilation
Set shComp = Worksheets.Add
'on les copie dans la feuille de compilation
'on admet ici, qu'ils débutent tous en cellule "A1" et qu'ils possedent chacun dix colonnes
'on copie d'abord les en-têtes
Sheets("sh1").Range("A1:J1").Copy shComp.Range("A1")
For Each sh In shName
R = Sheets(sh).Range("A1").CurrentRegion.Rows.Count - 1
Sheets(sh).Range("A2:J" & R).Copy
shComp.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
Next sh
'on crée une table pivot!
Set PVT = ActiveWorkbook.PivotCaches.Create(xlDatabase, shComp.Name & "!" & Range("A1").CurrentRegion.Address). _
CreatePivotTable(shComp.Name & "!R1C12", "PivotTableCompil")
PVT.PivotFields("DT").Orientation = xlRowField
PVT.PivotFields("DT").Position = 1
PVT.AddDataField PVT.PivotFields("A"), "Sum of A", xlSum
PVT.AddDataField PVT.PivotFields("B"), "Sum of B", xlSum
PVT.AddDataField PVT.PivotFields("C"), "Sum of C", xlSum
PVT.AddDataField PVT.PivotFields("D"), "Sum of D", xlSum
'...
PVT.PivotFields("DT").AutoSort xlAscending, "DT"
End Sub |
Partager