TCD avec macro

Bonjour à toutes et à tous,

j'ai fait une macro qui généré un TCD automatiquement a partir d'un fichier Excel

hors, la macro ne s’exécute pas et bug. elle ne s’exécute parfaitement que lorsque je supprime une des feuilles du classeur

auriez vous une idée de ce qui bugg? je n'arrive pas a m'en dépêtrer...

en pièce jointe le fichier

merci de votre aide!

Christine

Bonjour,

Essaye peut-être de voir en renommant ta feuille où ton TCD s'insère.

Sinon, quand tu debug, quelle partie du code est surlignée ?

Merci, j'ai essayé et ca ne fonctionne pas...

Quelle partie du code pose problème ?

celle ci :

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _

"Etape 1 (2)!R2C2:R1048576C6", Version:=xlPivotTableVersion14). _

CreatePivotTable TableDestination:="Feuil2!R3C1", TableName:= _

"Tableau croisé dynamique1", DefaultVersion:=xlPivotTableVersion14

Le problème vient peut-être du nom du TCD, car je suppose que si tu refais l'opération en supprimant la feuille 2, le nom du TCD doit être le même.

Je pense qu'il faudrait définir un nom générique au TCD en début de macro.

Bonjour,

Voir fichier.

Cdlt.

Option Explicit

Public Sub CreatePivotTable()
Dim wb As Workbook
Dim wsData As Worksheet, wsPT As Worksheet
Dim rngPT As Range
Dim PTCache As PivotCache
Dim PT As PivotTable

    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With
    '-----------------------------------------------------
    Set wb = ActiveWorkbook
    Set wsData = wb.Worksheets("Etape 1 (2)")
    Set rngPT = wsData.Cells(2, 1).CurrentRegion
    '-----------------------------------------------------
    On Error Resume Next
    wb.Worksheets("TCD").Delete
    On Error GoTo 0
    '-----------------------------------------------------
    Application.DisplayAlerts = True
    '-----------------------------------------------------
    Set PTCache = wb.PivotCaches.Create _
                  (SourceType:=xlDatabase, _
                   SourceData:=rngPT)
    '-----------------------------------------------------
    Set wsPT = wb.Worksheets.Add
    wsPT.Name = "TCD"

    Set PT = PTCache.CreatePivotTable _
             (tabledestination:=wsPT.Cells(1), _
              TableName:="PT_1")
    '-----------------------------------------------------
    With PT
        .ManualUpdate = True
        .AddFields RowFields:=Array("NOM", "NOM2")
        With .PivotFields("AFFICHAGE")
            .Orientation = xlDataField
            .Position = 1
            .Function = xlSum
            .NumberFormat = "#,##0;[Red](#,##0);"
            .Caption = ChrW(931) & " AFFICHAGE"
        End With
        With .PivotFields("CLIC")
            .Orientation = xlDataField
            .Position = 2
            .Function = xlSum
            .NumberFormat = "#,##0;[Red](#,##0);"
            .Caption = ChrW(931) & " CLIC"
        End With
        With .PivotFields("TAUX DE CLIC")
            .Orientation = xlDataField
            .Position = 3
            .Function = xlSum
            .NumberFormat = "0.000%;[Red](0.000%);"
            .Caption = ChrW(931) & " TAUX DE CLIC"
        End With
        .RowAxisLayout xlTabularRow
        .PivotFields("NOM").Subtotals(1) = True
        .TableStyle2 = "PivotStyleMedium2"
        .ManualUpdate = False
    End With
    '-----------------------------------------------------
    ActiveWindow.DisplayGridlines = False
    '-----------------------------------------------------
    Set PT = Nothing
    Set PTCache = Nothing
    Set wsPT = Nothing: Set wsData = Nothing
    Set wb = Nothing

End Sub

Ouahh!! merci beaucoup, c'est top!!!!

merci a tous les deux de m'avoir repondu!

Re,

Alors, pense à clore le sujet.

Cdlt.

Rechercher des sujets similaires à "tcd macro"