Bonjour,
Je te renvoie le fichier avec des commentaires pour les 2 procédures.
A te relire pour un complément d'informations.
Cdlt.
Option Explicit
'Déclaration des variables au niveau module
Dim wb As Workbook
Dim wsTable As Worksheet, wsPT As Worksheet
Dim lo As ListObject
'Consolidation et synthèse
Private Sub cmdGO_Click()
'Déclaration des variables au niveau procédure
Dim ws As Worksheet
Dim pt As PivotTable
Dim lo As ListObject
Dim rStart As Range, Rng As Range, rngGroup As Range
Dim lCol As Long, lastRow As Long
'Optimisation procédure (gel affichage)
Application.ScreenUpdating = False
'Initialisation des variables
Set wb = ActiveWorkbook
Set wsTable = wb.Worksheets("CONSOLIDATION")
Set wsPT = wb.Worksheets("TCD")
'Tableau1
Set lo = wsTable.ListObjects(1)
With lo
'RAZ Tableau1 en conservant la mise en forme et les formules
If Not .DataBodyRange Is Nothing Then .DataBodyRange.Delete
'Cellule pour la copie des données (B5)
Set rStart = .InsertRowRange.Cells(2)
End With
lCol = 10
' Fusion des 2 plages de données
For Each ws In wb.Worksheets
If ws.Name <> wsTable.Name And ws.Name <> "TCD" Then
'Dernière ligne non vide de la colonne A=1
lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
'Plage de données à copier
Set Rng = ws.Cells(1).CurrentRegion.Offset(1).Resize(lastRow - 1, lCol)
Rng.Copy
rStart.PasteSpecial xlPasteValues
'Inscription nom feuille dans Tableau1
rStart.Offset(, -1).Resize(Rng.Rows.Count).Value = ws.Name
Application.CutCopyMode = False
'Prochaine cellule pour la copie suivante
Set rStart = lo.HeaderRowRange.Cells(2).Offset(lo.ListRows.Count + 1)
End If
Next ws
With wsPT
'Initialisation variable TCD
Set pt = .PivotTables(1)
With pt
'Actualisation TCD
.PivotCache.Refresh
'Groupement des données en mois et années
Set rngGroup = .PivotFields("DelaiDemande").DataRange
rngGroup.Cells(1).Group _
periods:=Array(False, False, False, False, True, False, True)
End With
'Activation feuille TCD
.Activate
End With
'RAZ des variables
Set rngGroup = Nothing: Set Rng = Nothing: Set rStart = Nothing
Set lo = Nothing
Set wsPT = Nothing: Set wsTable = Nothing
Set wb = Nothing
End Sub
'RAZ tableau et actualisation TCD
Private Sub cmdRaz_Click()
'Optimisation procédure (gel affichage)
Application.ScreenUpdating = False
'Initialisation des variables
Set wb = ActiveWorkbook
Set wsTable = wb.Worksheets("CONSOLIDATION")
Set wsPT = wb.Worksheets("TCD")
Set lo = wsTable.ListObjects(1)
'RAZ tableau en conservant la mise en forme et les formules
With lo
If Not .DataBodyRange Is Nothing Then .DataBodyRange.Delete
End With
'Actualisation TCD
wsPT.PivotTables(1).PivotCache.Refresh
'RAZ des variables
Set lo = Nothing
Set wsPT = Nothing: Set wsTable = Nothing
Set wb = Nothing
End Sub