Bonjour,
J'ai plusieurs tableaux croisés dynamiques basés sur les même données et qui présentent les mêmes champ de page, situés dans le même classeur.
J'ai trouvé du code dans un livre: il marche mais présente plusieurs problèmes:
1) il marche uniquement si on ne sélectionne qu'un seul élément dans le filtre du champ de page. Pour ce que j'ai à faire, il faudrait que je puisse sélectionner plusieurs éléments dans le filtre de champ.
2) Dans le cas où on sélectionne l'élément "(Tous)" dans l'un des tableaux croisés dynamiques du classeur, la macro remplace l'un des éléments du champ de page des autres TCD par "(All)".
J'ai essayé de rajouter une ligne avec une instruction du type pt.EnableMultiplePageItems = True mais cela ne marche pas...
Le code suivant est dans un module:
Le code suivant est dans l'objet ThisWorkbook:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Option Explicit Sub SynchPivotTables(BasePivot As PivotTable) 'Variable declaration Dim Pivot As PivotTable Dim Sheet As Worksheet Dim BasePage As PivotField Dim Page As PivotField Dim Itm As PivotItem 'Synchronize the Pivot Tables in the Active Sheet 'Set Sheet = ActiveSheet 'or loop through all the Pivot Tables in the active workbook For Each Sheet In ActiveWorkbook.Worksheets 'Loop through the Pivots in the worksheet For Each Pivot In Sheet.PivotTables 'Try to synch if is not the base pivot table If Not Pivot Is BasePivot Then 'Loop through the page fields of the Base Pivot For Each BasePage In BasePivot.PageFields 'Clear the old page value Set Page = Nothing 'Check if its in the pages of the Pivot 'Continue if error occurs On Error Resume Next Set Page = Pivot.PageFields(BasePage.Name) On Error GoTo 0 'Is it there? If Not Page Is Nothing Then 'Try to assign the current page On Error Resume Next Page.CurrentPage.Caption = BasePage.CurrentPagepage.Caption 'Succeed? If Page.CurrentPage <> BasePage.CurrentPage Then 'Try using the PivotItems collection For Each Itm In Page.PivotItems If Itm.Caption = BasePage.CurrentPage Then Page.CurrentPage = Itm.Caption End If Next Itm End If On Error GoTo 0 End If Next BasePage End If Next Pivot Next Sheet End Sub
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 Option Explicit Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 'This procedure goes in the ThisWorkbook module of the same workbook. If TypeName(Sh) = "Worksheet" Then On Error GoTo exiting: With Target.PivotTable Application.EnableEvents = False SynchPivotTables Target.PivotTable Application.EnableEvents = True End With End If exiting: End Sub
Merci pour vos réponses.
Partager