Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.
  • Initiateur de la discussion Initiateur de la discussion mimiaqu
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

M

mimiaqu

Guest
Bonjour à tous,

Je sollicite vos connaissances pour m’éclairer sur un problème qui m’empêche d'avancer,
Grâce a l'enregistreur de Macro j'ai créé un TCD sauf que celle ci ne marche pas et je comprend pas pourquoi (débutante en VBA).

Pourriez vous m'aidez à résoudre ce problème SVP
Toute autre amélioration de ce code serait également la bienvenue.

Bonne journée à vous tous
 

Pièces jointes

Re : Macro TCD

Bonjour,

tu as déjà un tcd nommé "tableau croisé dynamique1".... supprime le au préalable, préférable d'utiliser une variable objet, exemple ci-dessous pour la création :

Code:
Dim TCD As PivotTable
With Sheets("tcd")
    If .PivotTables.Count > 0 Then .PivotTables(1).TableRange2.Delete
    Set TCD = .PivotTableWizard(xlDatabase, Sheets("stat").Range("A1").CurrentRegion, .Range("A1"))
End With

te reste plus qu'à utiliser la variable TCD pour la mise en place des champs ....

bonne journée
@+
 
Dernière édition:
Re : Macro TCD

j'ai essayé avec votre solution mais ça bloque toujours, ça me met erreur 1004
Code:
Sub TCD()
'
' tcd Macro
'
  
  Dim TCD As PivotTable
With Sheets("tcd")
    If .PivotTables.Count > 1 Then .PivotTables(1).TableRange2.Delete
    Set TCD = .PivotTableWizard(xlDatabase, Sheets("stat").Range("A1").CurrentRegion, .Range("A1"))
End With
   
    With ActiveSheet.PivotTables("TCD").PivotFields("tg_nat")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("TCD").AddDataField ActiveSheet. _
        PivotTables("TCD").PivotFields("tg_nat"), _
        "Nombre de tg_nat", xlCount
    ActiveSheet.PivotTables("TCD").PivotFields( _
        "Nombre de tg_nat").Caption = "Occurences"
    ActiveSheet.PivotTables("TCD").AddDataField ActiveSheet. _
        PivotTables("TCD").PivotFields("tg_nat"), _
        "Nombre de tg_nat", xlCount
    With ActiveSheet.PivotTables("TCD").PivotFields( _
        "Nombre de tg_nat")
        .Caption = "Cumul"
        .Calculation = xlPercentRunningTotal
        .BaseField = "tg_nat"
        .NumberFormat = "0,00%"
    End With
    With ActiveSheet.PivotTables("TCD").PivotFields("Cumul")
        .NumberFormat = "0%"
    End With
    ActiveSheet.PivotTables("TCD").AddDataField ActiveSheet. _
        PivotTables("TCD").PivotFields("Q_compenser"), _
        "Nombre de Q_compenser", xlCount
    With ActiveSheet.PivotTables("TCD").PivotFields( _
        "Nombre de Q_compenser")
        .Caption = "Min de Q_compenser"
        .Function = xlMin
    End With
    Range("B6").Select
    ActiveSheet.PivotTables("TCD").PivotFields("tg_nat"). _
        AutoSort xlDescending, "Occurences", ActiveSheet.PivotTables( _
        "TCD").PivotColumnAxis.PivotLines(1), 1
End Sub
 
Re : Macro TCD

Re,

aarf autant pour moi, le test indiqué est erroné, tester si > 0 en fait...
Code:
Dim TCD As PivotTable
With Sheets("tcd")
    If .PivotTables.Count > 0 Then .PivotTables(1).TableRange2.Delete
    Set TCD = .PivotTableWizard(xlDatabase, Sheets("stat").Range("A1").CurrentRegion, .Range("A1"))
End With

attention le nom de variable doit être différent du nom de la procédure....

ensuite remplacer partout dans le code :
Code:
ActiveSheet.PivotTables("TCD")
par :
Code:
TCD
 
Re : Macro TCD

Re,

cela pourrait donner ceci, à tester :
Code:
Sub test()
Dim TCD As PivotTable
With Sheets("tcd")
    If .PivotTables.Count > 0 Then .PivotTables(1).TableRange2.Delete
    Set TCD = .PivotTableWizard(xlDatabase, Sheets("stat").Range("A1").CurrentRegion, .Range("A1"))
End With
With TCD
    With .PivotFields("tg_nat")
        .Orientation = xlRowField
        .Position = 1
    End With
    .AddDataField .PivotFields("tg_nat"), "Nombre de tg_nat", xlCount
    .PivotFields("Nombre de tg_nat").Caption = "Occurences"
    .AddDataField .PivotFields("tg_nat"), "Nombre de tg_nat", xlCount
    With .PivotFields("Nombre de tg_nat")
        .Caption = "Cumul"
        .Calculation = xlPercentRunningTotal
        .BaseField = "tg_nat"
        .NumberFormat = "0,00%"
    End With
    With .PivotFields("Cumul")
        .NumberFormat = "0%"
    End With
    .AddDataField .PivotFields("Q_compenser"), "Nombre de Q_compenser", xlCount
    With .PivotFields("Nombre de Q_compenser")
        .Caption = "Min de Q_compenser"
        .Function = xlMin
    End With
    .PivotFields("tg_nat").AutoSort xlDescending, "Occurences", .PivotColumnAxis.PivotLines(1), 1
End With
End Sub
 
Re : Macro TCD

Re,

avec ceci, j'obtiens le même résultat que dans ton fichier, sauf la présentation :

Code:
Sub test()
Dim TCD As PivotTable
With Sheets("tcd")
    If .PivotTables.Count > 0 Then .PivotTables(1).TableRange2.Delete
    Set TCD = .PivotTableWizard(xlDatabase, Sheets("stat").Range("A1").CurrentRegion, .Range("A1"))
End With
With TCD
    .AddDataField .PivotFields("tg_nat"), "Nombre de tg_nat", xlCount
    .PivotFields("Nombre de tg_nat").Caption = "Occurences"
    .AddDataField .PivotFields("tg_nat"), "Nombre de tg_nat", xlCount
    With .PivotFields("tg_nat")
        .Orientation = xlRowField
        .Position = 1
    End With
    With .PivotFields("Nombre de tg_nat")
        .Caption = "Cumul"
        .Calculation = xlPercentRunningTotal
        .BaseField = "tg_nat"
        .NumberFormat = "0,00%"
    End With
    With .PivotFields("Cumul")
        .NumberFormat = "0%"
    End With
    .AddDataField .PivotFields("Q_compenser"), "Nombre de Q_compenser", xlCount
    With .PivotFields("Nombre de Q_compenser")
        .Caption = "Min de Q_compenser"
        .Function = xlMin
    End With
        With .DataPivotField
        .Orientation = xlColumnField
        .Position = 1
    End With
    .PivotFields("tg_nat").AutoSort xlDescending, "Occurences", .PivotColumnAxis.PivotLines(1), 1
End With
End Sub

bon après midi
@+
 
Re : Macro TCD

Re,

en optimisant un peu :
Code:
Option Explicit
Sub test()
Dim TCD As PivotTable
With Sheets("tcd")
    If .PivotTables.Count > 0 Then .PivotTables(1).TableRange2.Delete
    Set TCD = .PivotTableWizard(xlDatabase, Sheets("stat").Range("A1").CurrentRegion, .Range("A1"))
End With
With TCD
    .AddDataField .PivotFields("tg_nat"), "Occurences", xlCount
    .AddDataField .PivotFields("tg_nat"), "Nombre de tg_nat", xlCount
    .AddDataField .PivotFields("Q_compenser"), "Min de Q_compenser", xlMin
    With .PivotFields("tg_nat")
        .Orientation = xlRowField
        .Position = 1
    End With
    With .PivotFields("Nombre de tg_nat")
        .Caption = "Cumul"
        .Calculation = xlPercentRunningTotal
        .BaseField = "tg_nat"
        .NumberFormat = "0%"
    End With
    With .DataPivotField
        .Orientation = xlColumnField
        .Position = 1
    End With
    .PivotFields("tg_nat").AutoSort xlDescending, "Occurences", .PivotColumnAxis.PivotLines(1), 1
End With
End Sub
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD
Assurez vous de marquer un message comme solution pour une meilleure transparence.

Discussions similaires

  • Question Question
Microsoft 365 macro TCD
Réponses
4
Affichages
366
Réponses
16
Affichages
2 K
  • Question Question
Microsoft 365 Problème de date
Réponses
5
Affichages
358
  • Question Question
Microsoft 365 Erreur TCD
Réponses
5
Affichages
679
Réponses
4
Affichages
586
Réponses
9
Affichages
746
Compte Supprimé 979
C
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…