Bonjour,

Grâce à un post précèdent et à mat955 j'ai pu faire mon TCD à la volée (voir https://www.developpez.net/forums/d2.../#post11931581).

Cependant, j'ai un petit souci. J'arrive à rajouter des lignes et des colonnes mais je n'arrive pas à ajouter plusieurs lignes ou colonnes ou autres en maitrisant l'ordonnancement de chaque entrée...

Pour récapituler, je crée d'abord une Table à la volée :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
' ====================Pivot Tables and Graphs for KPIs====================================================
 
'$$$$ Create "Insert Table" for dynamic table and future MANUFACTURING KPIs ------------------------------
ProjectTabName = "Manuf_" & nom
ActiveSheet.Unprotect
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$W$63:$AQ$66"), , xlYes).Name = _
    ProjectTabName
Range(ProjectTabName).Select
ActiveSheet.ListObjects(ProjectTabName).TableStyle = ""

Puis, je crée un TCD :


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
'Create "Pivot Table" for dynamic table and future MANUFACTURING KPIs ------------------------------------
'PivotTabDynTabName = "TABLE_" & ProjectTabName
Range(ProjectTabName).Select
'    ActiveWindow.SmallScroll Down:=-15
ActiveSheet.Unprotect
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    ProjectTabName, Version:=8).CreatePivotTable TableDestination:= _
    Range("Origin_PivotTab_Manuf"), TableName:="TABLE_" & ProjectTabName & "_Manuf", DefaultVersion:=8
 
'Add fields --------------------------------------------------------------------------------------------
With ActiveSheet.PivotTables("TABLE_" & ProjectTabName & "_Manuf")
    .PivotFields("Supplier Name").Orientation = xlRowField
    .PivotFields("Product/Component").Orientation = xlColumnField
    .PivotFields("Supplier Name").Orientation = xlRowField .Position = 1
    .PivotFields("Status").Orientation = xlRowField .Position = 2
 
'    .PivotFields("Status").Orientation = xlColumnField
'    .AddDataField.PivotFields ("Status"), "Nombre de Status", xlCount
 
    ActiveSheet.PivotTables("TABLE_" & ProjectTabName & "_Manuf").AddDataField ActiveSheet. _
        PivotTables("TABLE_" & ProjectTabName & "_Manuf").PivotFields("Status"), _
        "Nombre de Status", xlCount
Cependant je bug sur le code. Je bloque et ça plante sur ".position = x" avec une erreur de "nombre de paramètres..."

J'ai essayé bien sûr de me baser sur le code généré par l'enregistreur de macro, mais j'aimerai éviter les With, End With a répétition :

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
''''''''Sub Macro3()
'''''''''
''''''''' Macro3 Macro
'''''''''
''''''''
'''''''''
''''''''    With ActiveSheet.PivotTables("TABLE_Manuf_BSA_Manuf").PivotFields( _
''''''''        "Supplier Name")
''''''''        .Orientation = xlRowField
''''''''        .Position = 1
''''''''    End With
''''''''    With ActiveSheet.PivotTables("TABLE_Manuf_BSA_Manuf").PivotFields( _
''''''''        "Product/Component")
''''''''        .Orientation = xlColumnField
''''''''        .Position = 1
''''''''    End With
''''''''    With ActiveSheet.PivotTables("TABLE_Manuf_BSA_Manuf").PivotFields("Status")
''''''''        .Orientation = xlColumnField
''''''''        .Position = 2
''''''''    End With
''''''''    ActiveSheet.PivotTables("TABLE_Manuf_BSA_Manuf").AddDataField ActiveSheet. _
''''''''        PivotTables("TABLE_Manuf_BSA_Manuf").PivotFields("Status"), "Nombre de Status" _
''''''''        , xlCount
''''''''End Sub
 
End With
Qu'en pensez-vous ? Comment passer le position dans mon code ?

Merci d'avance pour votre aide.
Cdt
Aero31