Bonjour,

Quelqu'un pourrait me venir en aide ? J'ai un probleme sur mon code, j'ai une dizaine de tableaux croisés dynamiques à automatiser sur une meme feuille (Generalite). le probleme est au niveau de :Set rngData = wsData.Cells(1).CurrentRegion . Car après l'exécution j'ai ce message d'erreur : Erreur d'exécution '13': Incompatibilité de type.

Mais si je supprime toutes les autres colonnes en gardant que ces 3 variables, il me gere bien tous les TCD sur une meme page (comme je veux...). Par contre j'ai encore d'autre variables à y ajouter...
Merci, d'avance....

Je vous met l'extrait du 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
Option Explicit
Dim wsData As Worksheet, wsPT As Worksheet
Dim rngData As Range
Dim ptCache As PivotCache
Dim pt As PivotTable
Dim i As Byte
 
Public Sub TDC1()
 
'Généralité
 
    Application.ScreenUpdating = False
 
    Set wsData = Worksheets("Données")
    Set rngData = wsData.Cells(1).CurrentRegion
    Set wsPT = Worksheets("Generalite")
    'Suppression de tous les TCD existants dans la feuille
   For Each pt In wsPT.PivotTables
        pt.TableRange2.Clear
    Next pt
 
With wsPT
    Set ptCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, rngData, 4)
 
    'Diplômés par sexe :
 
    Set pt = ptCache.CreatePivotTable(wsPT.Range("B5"), "TCD_1", , 4)
 
 
   With pt
        .ManualUpdate = True
 
        ' Effectif ou nombre
 
       With .PivotFields("sexe")
            .Orientation = xlRowField
            .Orientation = xlDataField
            .Position = 1
            .Caption = "Effectif genre "
        End With
 
 
        With .PivotFields("sexe")
            .Orientation = xlRowField
            .Orientation = xlDataField
            .Calculation = xlPercentOfColumn
            .NumberFormat = "#,##0"
            .Position = 1
            .Caption = "Pourcentage genre(%) "
        End With
 
        .ManualUpdate = False
      End With
 
 
      Set pt = ptCache.CreatePivotTable(wsPT.Range("B15"), "TCD_2", , 4)
 
    With pt
        .ManualUpdate = True
        'Ajout d'une Colonne puis ajouter "genre" en valeur : .Orientation = xlDataField ???
       With .PivotFields("nationalite")
           .Orientation = xlRowField
            .Orientation = xlDataField
            .Position = 1
            .NumberFormat = "#,##0"
            .Caption = "Effectif "
        End With
 
 
        With .PivotFields("nationalite")
            .Orientation = xlRowField
            .Orientation = xlDataField
            .Calculation = xlPercentOfColumn
            .NumberFormat = "#,##0"
            .Position = 1
            .Caption = "Pourcentage nationalité "
        End With
        .ManualUpdate = False
    End With
 
    Set pt = ptCache.CreatePivotTable(wsPT.Range("F5"), "TCD_3", , 4)
    With pt
        .ManualUpdate = True
 
        With .PivotFields("regime")
            .Orientation = xlRowField
            .Orientation = xlDataField
            .Position = 1
            .NumberFormat = "#,##0"
            .Caption = "Effectif "
        End With
 
 
        With .PivotFields("nationalite")
            .Orientation = xlRowField
            .Orientation = xlDataField
            .Calculation = xlPercentOfColumn
            .NumberFormat = "#,##0"
            .Position = 1
            .Caption = "Pourcentage regime "
        End With
       .ManualUpdate = False
    End With
 
End With
 
    Set pt = Nothing
    Set ptCache = Nothing
    Set rngData = Nothing
    Set wsPT = Nothing: Set wsData = Nothing
 
End Sub