Bonjour,

J'ai commencé à construire une macro mais la je bloque. Je vais essayer de faire simple.

Objectif : Créer à partir de la feuille "Donnees" une feuille pour chaque exploitant. Sur chaque feuille je veux créer trois TCD avec les données de la feuille "Donnees".

Environnement
Mes données pour créer le TCD sont dans la feuilles "Donnees", de A à AJ, avec un nombres de lignes différents par mois.
Il y à une feuille "Exploitants" avec la liste des exploitants variables selon le mois. (environ 15)

J'ai donc fais (en gros) une boucle qui dit que tant que la colonne où sont les exploitants n'est pas vide tu fais :
- Créer une feuille.
- Créer 3 TCD.

Mon problème : La feuille se crée, les 3 TCD aussi, c'est selon lorsque je passe à la seconde feuille, donc aux second exploitants, que le premier TCD se crée mais bloque lors de premier parametres. (Erreur 1004 : Impossible de lire la propriété PivotTables de la classe Worksheet)

J'ai été voir sur internet essayer plusieurs choses mais ça bloque.

Voici mon code :
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
Sub trere()
'###################
'CREATION TCD
'###################
Dim exploit
Dim fexploit
i = 3
n = 1
TCD = "1"
TCD2 = "2"
TCD3 = "3"
 
Sheets("Exploitants").Select
 
    While Range("A" & n) <> ""
        exploit = Range("A" & n)
        fexploit = Range("B" & n)
 
    Sheets.Add After:=Sheets(Sheets.Count)
   ' ActiveSheet.Name = "Feuil" & i
 
'#
'Tableau TCD numéro 1
'#
 
'Création du tableau:
 
 
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
    [Donnees!A1].CurrentRegion.Address(, , xlR1C1, True)).CreatePivotTable _
    TableDestination:="Feuil" & i & "!R6C1", _
    TableName:=TCD
 
'Mise en forme du tableau :
With Worksheets("Feuil" & i).PivotTables(TCD).PivotFields( _
        "Exploit")
        .Orientation = xlPageField
        .Position = 1
     Worksheets("Feuil" & i).PivotTables(TCD).PivotFields("Exploit"). _
        CurrentPage = "/gfgfgf/fgfgft/gfgfgf/Nsso/Sspo/Mm/"
End With
With Worksheets("Feuil" & i).PivotTables(TCD).PivotFields( _
        "Maestro Label")
        .Orientation = xlPageField
        .Position = 1
End With
With Worksheets("Feuil" & i).PivotTables(TCD).PivotFields( _
        "APPLICATION")
        .Orientation = xlRowField
        .Position = 1
End With
With Worksheets("Feuil" & i).PivotTables(TCD).PivotFields("Object")
        .Orientation = xlRowField
        .Position = 2
End With
 
        Worksheets("Feuil" & i).PivotTables(TCD).AddDataField Worksheets("Feuil" & i).PivotTables(TCD). _
        PivotFields("Alarm Text"), "Nombre de Alarm Text", xlCount
 
 
Range("A7").Select
Selection.ShowDetail = False
 
TCD = TCD + 3
 
 
'#
'Tableau TCD numéro 2
'#
 
Range("E5").Select
'Création du tableau:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
    [Donnees!A1].CurrentRegion.Address(, , xlR1C1, True)).CreatePivotTable _
    TableDestination:="Feuil" & i & "!R6C5", _
    TableName:=TCD2
 
'Mise en forme du tableau :
With Worksheets("Feuil" & i).PivotTables(TCD2).PivotFields( _
        "Exploit")
        .Orientation = xlPageField
        .Position = 1
     Worksheets("Feuil" & i).PivotTables(TCD2).PivotFields("Exploit"). _
        CurrentPage = "/gfgfgf/fgfgft/gfgfgf/Nsso/Sspo/Mm/"
End With
With Worksheets("Feuil" & i).PivotTables(TCD2).PivotFields( _
        "Maestro Label")
        .Orientation = xlPageField
        .Position = 1
End With
With Worksheets("Feuil" & i).PivotTables(TCD2).PivotFields( _
        "Superieur 6mins")
        .Orientation = xlPageField
        .Position = 1
    Worksheets("Feuil" & i).PivotTables(TCD2).PivotFields( _
        "Maestro Label").CurrentPage = "Flux_Generique"
End With
With Worksheets("Feuil" & i).PivotTables(TCD2).PivotFields( _
        "APPLICATION")
        .Orientation = xlRowField
        .Position = 1
End With
With Worksheets("Feuil" & i).PivotTables(TCD2).PivotFields("Object")
        .Orientation = xlRowField
        .Position = 2
End With
        Worksheets("Feuil" & i).PivotTables(TCD2).AddDataField Worksheets("Feuil" & i).PivotTables(TCD2). _
        PivotFields("Alarm Text Bis"), "Nombre de Alarm Text Bis", xlCount
 
        Worksheets("Feuil" & i).PivotTables(TCD2).AddDataField Worksheets("Feuil" & i).PivotTables(TCD2). _
        PivotFields("Oceane ID Bis"), "Nombre de Oceane ID Bis", xlCount
 
        Worksheets("Feuil" & i).PivotTables(TCD2).AddDataField Worksheets("Feuil" & i).PivotTables(TCD2). _
        PivotFields("Maestro Label Bis"), "Nombre de Maestro Label Bis", xlCount
With Worksheets("Feuil" & i).PivotTables(TCD2).DataPivotField
        .Orientation = xlColumnField
        .Position = 1
End With
 
TCD2 = TCD2 + 3
 
'#
'Tableau TCD numéro 3
'#
 
Range("K5").Select
'Création du tableau:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
    [Donnees!A1].CurrentRegion.Address(, , xlR1C1, True)).CreatePivotTable _
    TableDestination:="Feuil" & i & "!R6C11", _
    TableName:=TCD3
 
'Mise en forme du tableau :
With Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields( _
        "Exploit")
        .Orientation = xlPageField
        .Position = 1
     Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields("Exploit"). _
        CurrentPage = "/gfgfgf/fgfgft/gfgfgf/Nsso/Sspo/Mm/"
End With
With Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields( _
        "Maestro Label")
        .Orientation = xlPageField
        .Position = 1
 
    Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields("Maestro Label").CurrentPage = "(All)"
End With
With Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields("Maestro Label")
        .PivotItems("Flux_Generique").Visible = False
End With
With Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields( _
        "Superieur 6mins")
        .Orientation = xlPageField
        .Position = 1
 
    Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields( _
        "Superieur 6mins").CurrentPage = "supérieur à 6 min"
End With
With Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields( _
        "PFS")
        .Orientation = xlRowField
        .Position = 1
End With
With Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields( _
        "APPLICATION")
        .Orientation = xlRowField
        .Position = 2
End With
With Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields("Object Class")
        .Orientation = xlRowField
        .Position = 3
End With
With Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields("Parameter Name")
        .Orientation = xlRowField
        .Position = 4
End With
        Worksheets("Feuil" & i).PivotTables(TCD3).AddDataField Worksheets("Feuil" & i).PivotTables(TCD3). _
        PivotFields("Alarm Text Bis"), "Nombre de Alarm Text Bis", xlCount
 
        Worksheets("Feuil" & i).PivotTables(TCD3).AddDataField Worksheets("Feuil" & i).PivotTables(TCD3). _
        PivotFields("Oceane ID Bis"), "Nombre de Oceane ID Bis", xlCount
 
With Worksheets("Feuil" & i).PivotTables(TCD3).DataPivotField
        .Orientation = xlColumnField
        .Position = 1
End With
    Worksheets("Feuil" & i).PivotTables(TCD3).CalculatedFields.Add _
        "Ratio", "='Alarm Text Bis' /'Oceane ID Bis'", True
    Worksheets("Feuil" & i).PivotTables(TCD3).PivotFields("Ratio"). _
        Orientation = xlDataField
 
Range("K7").Select
Selection.ShowDetail = False
 
'#
'INSERTION COULEUR RATIO
'#
 
    Worksheets("Feuil" & i).PivotTables(TCD3).PivotSelect _
        "'Somme de Ratio' 'exploit' 'supérieur à 6 min'", _
        xlDataAndLabel, True
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
        Formula1:="=2,01"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16752384
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13561798
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
 
 
TCD3 = TCD3 + 3
ActiveSheet.Name = fexploit
i = i + 1
n = n + 1
Sheets("Exploitants").Select
Wend
 
End Sub
J'espere que j'ai été clair, si des personnes veulent bien m'aider j'en serais ravie.
Merci beaucoup par avance