Option Base 1
Private Sub Workbook_SheetPivotTableUpdate(ByVal sh As Object, ByVal Target As PivotTable)
Dim Ninsert&, espace&, a(), i&, col%, j&, z&
Ninsert = 50 'à adapter
Application.ScreenUpdating = False
ReDim a(1 To sh.PivotTables.Count, 3)
'Condition ou Liste des onglets non concernés
If UBound(a, 1) = 1 Then Exit Sub 'il y a seulement un TCD dans la feuille
If sh.Name = "ACHATS" Then Exit Sub
If sh.Name = "ANALYSE BUDGET" Then Exit Sub
'Espacement TCD
With sh
.Cells.EntireRow.Hidden = False
For i = 1 To UBound(a, 1)
a(i, 1) = .PivotTables(i).TableRange2.Row
a(i, 2) = .PivotTables(i).TableRange2.Rows.Count
Next
Call tri(a(), 1, UBound(a, 1), 2, 1)
'Insertion ou suppression de lignes---
For i = 1 To UBound(a, 1) - 1
espace = a(i + 1, 1) - (a(i, 1) + a(i, 2))
If espace < Ninsert Then a(i, 3) = Ninsert - espace Else a(i, 3) = Ninsert - espace
Next
For i = UBound(a, 1) To 2 Step -1
z = a(i - 1, 3)
If z > 0 Then
.Rows(a(i - 1, 1) + a(i - 1, 2)).Resize(z).Insert
ElseIf z < 0 Then
z = -z
.Rows(a(i - 1, 1) + a(i - 1, 2)).Resize(z).EntireRow.Delete
End If
'Masquage
.Rows(a(i - 1, 1) + a(i - 1, 2) & ":" & a(i, 1) + a(i - 1, 3) - 7).EntireRow.Hidden = True
Next
NB_TCD2 = NB_TCD2 + 1
If NB_TCD2 = NB_TCD Then
MsgBox "Actualisation terminée !", vbInformation, "INFORMATION GardenManager"
.Protect "", DrawingObjects:=True, Contents:=True, Scenarios:= _
True, UserInterfaceOnly:=True, AllowFormattingRows:=False, AllowInsertingColumns:=False, _
AllowInsertingRows:=False, AllowInsertingHyperlinks:=True, _
AllowDeletingColumns:=False, AllowDeletingRows:=False, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If
End With
End Sub