Macro TCD - Erreur sur erreur
Bonjour,
Merci à ceux qui prendront le temps de lire et de répondre à ce énième post de ma part.
J'ai encore une fois, un problème avec VBA. Je souhaite automatisé la création d'un TCD à partir d'une base de donnée, venant elle même d'une autre base (Filtre élaboré faite sur celle-ci avec copiage sur une nouvelle feuille).
Mon problème vient de la macro pour créer le TCD. Je souhaite qu'il se crée sur une feuille déjà existante.
J'ai essayé bon nombre de macro sur internet, d'écritures différentes, mais rien n'y fait ! J'ai toujours un message d'erreur qui s'affiche et lorsque je règle le problème un nouveau apparaît !
Actuellement, j'ai deux messages erreurs sur ma macro :
- "xlRowField" : erreur de compilation : qualificateur incorrect, surligné en bleu.
- "Set objTable = Sheet2.PivotTableWizard" est également surligné en jaune.
Sub TCD()
'
' TCD Macro
' Macro enregistrée le 10/05/2016 par lycée
' Creates a PivotTable report from the table on Sheet2
' by using the PivotTableWizard method with the PivotFields
' method to specify the fields in the PivotTable.
Dim objTable As PivotTable, objField As PivotField
' Select the sheet and first cell of the table that contains the data.
ActiveWorkbook.Sheets("Liste").Select
Range("A1").Select
' Create the PivotTable object based on the Liste data on Sheet2.
Set objTable = Sheet2.PivotTableWizard
' Specify row and column fields.
Set objField = objTable.PivotFields("OPTION 4")
objField.Orientation = xlRowField.Position = 1
Set objField = objTable.PivotFields("SEXE")
objField.Orientation = xlRowField.Position = 2
Set objField = objTable.PivotFields("OPTION 5")
objField.Orientation = xlColumnField.Position = 1
' Specify a data field with its summary
' function and format.
Set objField = objTable.PivotFields("NOM")
objField.Orientation = xlDataField
objField.Function = xlSum
objField.NumberFormat = "$ #,##0"
' Specify a page field.
Set objField = objTable.PivotFields("NOM")
objField.Orientation = xlPageField
End Sub
Merci à ceux qui m'apporteront leur aide.
Bonjour,
Essaie ainsi :
Option Explicit
Public Sub TCD()
Dim wb As Workbook
Dim ws As Worksheet, ws2 As Worksheet
Dim PTCache As PivotCache
Dim pt As PivotTable
Dim rngPT As Range
Application.ScreenUpdating = False
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Liste")
Set rngPT = ws.Cells(1).CurrentRegion
Set ws2 = wb.Worksheets("Tableau")
On Error Resume Next
ws2.PivotTables(1).TableRange2.Clear
On Error GoTo 0
Set PTCache = wb.PivotCaches.Add _
(SourceType:=xlDatabase, _
SourceData:=rngPT)
Set pt = PTCache.CreatePivotTable _
(tabledestination:=ws2.Cells(6, 2), _
TableName:="TCD_1", _
defaultversion:=xlPivotTableVersion10)
With pt
.ManualUpdate = True
.AddFields RowFields:=Array("OPTION 4", "SEXE"), _
ColumnFields:="OPTION 5"
With .PivotFields("NOM")
.Orientation = xlDataField
.Function = xlCount
.NumberFormat = "#,##0"
.Caption = "NB NOMS"
End With
.ManualUpdate = False
End With
wb.ShowPivotTableFieldList = False
With ws2
.Activate
.[A1].Select
End With
Set rngPT = Nothing
Set pt = Nothing
Set PTCache = Nothing
Set ws2 = Nothing: Set ws = Nothing
Set wb = Nothing
End SubMerci beaucoup ! Ca marche parfaitement, aucune ligne d'erreur, rien !
Peux-tu m'expliquer ce que tu as modifié afin que je puisse de sortir du pétrin toute seule la prochaine fois ?
Merci encore !!
Re,
Tu auras remarqué que j'ai tout modifié ou presque.
J'ai commenté le code pour te permettre (peut-être) de reproduire la procédure.
A te relire si tu as besoin d'un complément d'informations.
Cette nouvelle procédure masque les lignes (vide).
Cordialement.
Option Explicit
Public Sub TCD()
' Déclaration des variables
Dim wb As Workbook
Dim ws As Worksheet, ws2 As Worksheet
Dim PTCache As PivotCache
Dim pt As PivotTable
Dim rngPT As Range
' Optimisation (gel affichage)
Application.ScreenUpdating = False
' Initialisation des variables
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Liste")
Set rngPT = ws.Cells(1).CurrentRegion 'Données source du TCD
Set ws2 = wb.Worksheets("Tableau")
' Suppression TCD
On Error Resume Next
ws2.PivotTables(1).TableRange2.Clear
On Error GoTo 0
' Création du cache de TCD (à partir de rngPT)
Set PTCache = wb.PivotCaches.Add _
(SourceType:=xlDatabase, _
SourceData:=rngPT)
' Création du TCD en feuille Tableau nommé TCD_1
Set pt = PTCache.CreatePivotTable _
(tabledestination:=ws2.Cells(6, 2), _
TableName:="TCD_1", _
defaultversion:=xlPivotTableVersion10)
With pt
' Calcul TCD manuel(Optimmisation)
.ManualUpdate = True
' Ajout des étiquettes de lignes et de colonnes
.AddFields RowFields:=Array("OPTION 4", "SEXE"), _
ColumnFields:="OPTION 5"
' Ajout champ valeurs
With .PivotFields("NOM")
.Orientation = xlDataField
.Function = xlCount
.NumberFormat = "#,##0"
.Caption = "NB NOMS"
End With
' Calcul automatique (affiche le TCD)
.ManualUpdate = False
' Suppression des (vide)
On Error Resume Next
.PivotFields("OPTION 4").PivotItems("(Blank)").Visible = False
On Error GoTo 0
End With
' Affichage de la liste de champs du TCD (False ou True)
wb.ShowPivotTableFieldList = False
'
With ws2
.Activate
.[A1].Select
End With
' RAZ des variables
Set rngPT = Nothing
Set pt = Nothing
Set PTCache = Nothing
Set ws2 = Nothing: Set ws = Nothing
Set wb = Nothing
End SubMerci beaucoup à toi !
Je vais l'insérer de cette façon dans ma macro comme ça je comprendrais les démarches pour la (re)réaliser.
Bonne journée a toi, et encore merci pour ton aide.