Re,
Bonjour R@chid,
Si tu as regardé la procédure VBA, tu auras remarqué que le fonctionnement est pour un tableau de 2 colonnes...
Tu ne pourras pas l'intégrer telle que si tes données réelles comporte un grand nombre de colonnes.
Pour avoir la somme au lieu de la moyenne, remplacer xlAverage par xlSum.
La prochaine fois, envoie un fichier représentatif de tes données.
Cdlt.
Option Explicit
Public Sub Consolidate_Data()
Dim wb As Workbook
Dim ws As Worksheet
Dim tbl As Variant, arr() As Variant
Dim rng As Range
Dim I As Long
Dim pc As PivotCache
Dim pt As PivotTable
Dim lo As ListObject
Application.ScreenUpdating = False
Set wb = ActiveWorkbook
Set ws = ActiveSheet
ws.Columns("C:K").Delete Shift:=xlToLeft
tbl = ws.ListObjects(1).Range
ReDim arr(1 To UBound(tbl), 1 To 2)
arr(1, 1) = "Date"
arr(1, 2) = "valeur"
For I = 2 To UBound(tbl)
arr(I, 1) = Year(CDate(tbl(I, 1))) & "-" & Format(Month(CDate(tbl(I, 1))), "00")
arr(I, 2) = tbl(I, 2)
Next I
ws.Cells(4).Resize(UBound(tbl), UBound(arr, 2)).Value = arr
Set rng = ws.Cells(4).Resize(UBound(tbl), UBound(arr, 2))
Set pc = wb.PivotCaches.Create(xlDatabase, rng)
Set pt = pc.CreatePivotTable(ws.Cells(7), "PT_1")
With pt
.AddFields RowFields:="Date"
With .PivotFields("Valeur")
.Orientation = xlDataField
.Function = xlAverage
.NumberFormat = "#,##0.00;[Red]-#,##0.00;"
.Caption = "Moyenne"
End With
.RowAxisLayout xlTabularRow
.ColumnGrand = False
End With
pt.TableRange1.Copy
ws.Cells(10).PasteSpecial xlPasteValuesAndNumberFormats
Set lo = ws.ListObjects.Add(xlSrcRange, ws.Cells(10).CurrentRegion, , xlYes)
With lo
.Name = "Tableau2"
.TableStyle = "TableStyleLight1"
.ShowTableStyleRowStripes = False
End With
ws.Columns("D:I").Delete Shift:=xlToLeft
ws.Cells(1).Select
End Sub