bouclesdor
XLDnaute Occasionnel
Bonjour à vous les pros d'excel et de VBA,
J'aimerais créer un tableau dynamique croisé (pivottable) avec un code VBA donc comme je suis débutante en VBA, j'ai voulu enregistrer le code avec l'enregistreur de macro. Je pensais que ça serait super simple d'intégrer ce code à un bouton pour qu'ensuite le tableau dynamique soit généré automatiquement.
Cependant, lorsque j'essaie de faire rouler la macro après l'avoir enregistré, j'ai un message d'erreur "Run-time error 5: invalid procedure call or argument". Ce qui ce met en jaune et ce bout de code:
ActiveWorkbook.Worksheets("Type vente").PivotTables("TestPivot").PivotCache. _
CreatePivotTable TableDestination:= _
"'[STEPH chart Analyse des ventes.xlsm]type vente chart'!R1C1", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion12
Pourquoi??
En résumé, j'ai déjà un tableau dynamique croisé (TDC) dans mon onglet " type vente" et mon TDC se nomme: "TestPivot" je veux donc prendre se TDC et en créer un autre dans un autre onglet nommé: "type vente chart" et ce à partir du TDC existant dans mon premier onglet "type vente".
Est-ce que quelqu'un peu me donner un indice pourquoi j'ai cet erreur car comme mon code a été créé avec le macro recorder je pensais qu'il n'y aurait pas d'erreur dans le code...
Merci à l'avance de votre aide...
Bouclesdor
Voici mon code complet si ça peu vous aider...
Sub steph111()
Range("A1").Select
ActiveWorkbook.Worksheets("Type vente").PivotTables("TestPivot").PivotCache. _
CreatePivotTable TableDestination:= _
"'[STEPH chart Analyse des ventes.xlsm]type vente chart'!R1C1", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion12
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Année")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Type de vente")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Regroupement")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Prix (CDN)"), "Count of Prix (CDN)", xlCount
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Count of Prix (CDN)")
.Caption = "Sum of Prix (CDN)"
.Function = xlSum
.Calculation = xlPercentOfRow
.NumberFormat = "0.00%"
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Année")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Type de vente")
.Orientation = xlColumnField
.Position = 1
End With
End Sub
J'aimerais créer un tableau dynamique croisé (pivottable) avec un code VBA donc comme je suis débutante en VBA, j'ai voulu enregistrer le code avec l'enregistreur de macro. Je pensais que ça serait super simple d'intégrer ce code à un bouton pour qu'ensuite le tableau dynamique soit généré automatiquement.
Cependant, lorsque j'essaie de faire rouler la macro après l'avoir enregistré, j'ai un message d'erreur "Run-time error 5: invalid procedure call or argument". Ce qui ce met en jaune et ce bout de code:
ActiveWorkbook.Worksheets("Type vente").PivotTables("TestPivot").PivotCache. _
CreatePivotTable TableDestination:= _
"'[STEPH chart Analyse des ventes.xlsm]type vente chart'!R1C1", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion12
Pourquoi??
En résumé, j'ai déjà un tableau dynamique croisé (TDC) dans mon onglet " type vente" et mon TDC se nomme: "TestPivot" je veux donc prendre se TDC et en créer un autre dans un autre onglet nommé: "type vente chart" et ce à partir du TDC existant dans mon premier onglet "type vente".
Est-ce que quelqu'un peu me donner un indice pourquoi j'ai cet erreur car comme mon code a été créé avec le macro recorder je pensais qu'il n'y aurait pas d'erreur dans le code...
Merci à l'avance de votre aide...
Bouclesdor
Voici mon code complet si ça peu vous aider...
Sub steph111()
Range("A1").Select
ActiveWorkbook.Worksheets("Type vente").PivotTables("TestPivot").PivotCache. _
CreatePivotTable TableDestination:= _
"'[STEPH chart Analyse des ventes.xlsm]type vente chart'!R1C1", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion12
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Année")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Type de vente")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Regroupement")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Prix (CDN)"), "Count of Prix (CDN)", xlCount
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Count of Prix (CDN)")
.Caption = "Sum of Prix (CDN)"
.Function = xlSum
.Calculation = xlPercentOfRow
.NumberFormat = "0.00%"
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Année")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Type de vente")
.Orientation = xlColumnField
.Position = 1
End With
End Sub