Automatisation d'un création d'un TCD d'une macro
Bonjour (Bonsoir) à tous,
je souhaite créer automatiquement un tableau dynamique croisé(TCD) à l'aide d'une macro mais hélas j'ai toujours une erreur au niveau de SourceData (la source de données). J'essaie plusieurs méthodes et formulations, rien!
Voici mon exmple de mon fichier excel avec ma macro : https://www.cjoint.com/c/EKbq1lTKgVS ou ci-joint.
Mon deuxième problème est que je souhaiterai obtenir un champs de valeur avec de cellules colorier à la place de (nombre ou de somme), "dans ma macro je l'ai programmé dans la mise en forme de TCD.
Merci d'avancé pour votre aide et bonne soirée.
Bonjour,
Je n'ai pas tout saisi, mais tu trouveras ci-dessous une procédure pour créer ton TCD.
Cdlt.
Option Explicit
Public Sub CreatePT()
Dim wb As Workbook
Dim wsData As Worksheet, wsPT As Worksheet
Dim rngData As Range
Dim PTCache As PivotCache
Dim pt As PivotTable
Application.ScreenUpdating = False
Set wb = ThisWorkbook
With wb
Set wsData = .Worksheets("Boutique1")
Set wsPT = .Worksheets("Analyse")
End With
On Error Resume Next
wsPT.PivotTables(1).TableRange2.Clear
On Error GoTo 0
Set rngData = wsData.Cells(1).CurrentRegion
Set PTCache = wb.PivotCaches.Create(xlDatabase, rngData, 3)
Set pt = PTCache.CreatePivotTable(wsPT.Cells(1), "TCD", , 3)
With pt
.ManualUpdate = True
.AddFields ColumnFields:="Analysée", RowFields:="Utilisation"
With .PivotFields("Code")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0;-#,##0;"
.Caption = ChrW(931) & " Color"
End With
.RowAxisLayout xlTabularRow
.ColumnGrand = False
.RowGrand = False
.ManualUpdate = False
With .DataBodyRange
.FormatConditions.Delete
.FormatConditions.Add xlCellValue, xlEqual, "=1" 'Oui
.FormatConditions(1).Interior.ColorIndex = 6
.FormatConditions.Add xlCellValue, xlEqual, "=0" 'Non
.FormatConditions(2).Interior.ColorIndex = 2
.FormatConditions.Add xlCellValue, xlEqual, "=30" 'Sans objet
.FormatConditions(3).Interior.ColorIndex = 4
.FormatConditions.Add xlCellValue, xlEqual, "=-8" 'Vide
.FormatConditions(4).Interior.ColorIndex = 3
.FormatConditions.Add xlNoBlanksCondition 'Plusieurs
.FormatConditions(5).Interior.ColorIndex = 2
End With
End With
With wsPT
.Activate
.[A1].Select
End With
Set pt = Nothing
Set PTCache = Nothing
Set rngData = Nothing
Set wsPT = Nothing: Set wsData = Nothing
Set wb = Nothing
End SubRé-Bonjour Jean-Eric,
Merci tout d'abord pour ton aide, ta réponse répond à 90% de ma question. Désolé, je vais un peu abuser avec mes questions.
J'aurai adapté ton programme avec mon vrai fichier. Ce fichier comprend en colonnes (A:J) et ligne (2:5096)
J'essaie d'apater avec ton programme, je n'obtiens rien comme champs de valeurs dans mon TCD.
'Ces 3 premiers déclarations que je ne comprends pas trop. Si j'adapte avec mon fichier EXEMPLE joint: la colonne code se déplace en H et celle Color en J.
Set rngData = wsData.Cells(1).CurrentRegion
Set PTCache = wb.PivotCaches.Create(xlDatabase, rngData, 3)
Set pt = PTCache.CreatePivotTable(wsPT.Cells(1), "TCD", , 3)
With pt
.ManualUpdate = True
.AddFields ColumnFields:="Analysée", RowFields:="Utilisation"
With .PivotFields("Code")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0;-#,##0;"
.Caption = ChrW(931) & " Color"
End With
Merci d'avancé et Bien Cordialement.
RE,
N'as-tu oublié de joindre ton fichier Exemple?
Cdlt.
Voici mon fichier original.
Les données qui vont changer sont les suivants :
.ManualUpdate = True
.AddFields ColumnFields:="ZOO", RowFields:="VANDE"
With .PivotFields("ZIC")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0;-#,##0;"
.Caption = ChrW(931) & " CODE"
End With
Merci d'avancé et bonne soirée.
Bien Cordialement.
Re,
Pas trouvé de ZIC, j'ai donc supposé que le champ des valeurs correspondait à IN26.
Attention, j'ai dû convertir ce champ texte en valeurs numériques.
Le code est commenté.
Cdlt.
Bonjour Jean-Eric,
Merci beaucoup pour ton aide c'est gentil de ta part. C'est parfait, la dernière question pour clore le sujet. Comment déplace le bouton de lancement nommé "Créer mon TCD" sur la feuille "Boutique1". C'est à dire si je devrais adapter le programme (ou le code) avec un autre fichier excel (ou un exemple)? Parce que quand je fais copier-coller du code, le bouton ne se déplace (bouge) pas.