[VBA]Tableaux croisé dynamique avec source variable

Résolu
propagandeman Messages postés 6 Date d'inscription vendredi 14 octobre 2005 Statut Membre Dernière intervention 21 novembre 2011 - 16 nov. 2011 à 17:46
propagandeman Messages postés 6 Date d'inscription vendredi 14 octobre 2005 Statut Membre Dernière intervention 21 novembre 2011 - 21 nov. 2011 à 18:03
Bonjour,

J'ai recherché sur internet une solution à ma requête mais sans succès. Je n'ai peut être pas compris toutes les subtilités de certains codes mais soit.

Il s'agit de tableau croisé dynamique.

Voici un code "lambda" de TCD:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"[sapoV21.xlsx]cartouches!R1C7:R1048576C7", Version:=xlPivotTableVersion12). _
CreatePivotTable TableDestination:="Sheet1!R2C12", TableName:="PivotTable2" _
, DefaultVersion:=xlPivotTableVersion12

Pour moi il y a 2 informations :
La source : "[sapoV21.xlsx]cartouches!R1C7:R1048576C7"
La destination : "Sheet1!R2C12"

Je cherche à rendre la source variable en fonction de l'adresse dans la cellule B14 de l'onglet DATA'! au format :
'Q:\LOGISTIQUE\[sapoV21.xlsx]cartouches'!G:G

La destination resterait dans notre exemple "Sheet1!R2C12"

J'ai testé quelques trucs avec Application.WorksheetFunction.indirect() mais je n'y arrive pas (surement un problème de synthaxe)
De plus, le code TDC est en R1C1 automatiquement plutôt qu'en A1, je ne sais pas si ça joue.

Je ne suis pas encore un expert en VBA, c'est pourquoi je recherche quelque chose de relativement simple et pédagogue.

Peut être du genre :

i = ("Data'!B14").value

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"i", Version:=xlPivotTableVersion12). _
CreatePivotTable TableDestination:="Sheet1!R2C12", TableName:="PivotTable2" _
, DefaultVersion:=xlPivotTableVersion12


Merci d'avance pour vos conseils et explications.

8 réponses

propagandeman Messages postés 6 Date d'inscription vendredi 14 octobre 2005 Statut Membre Dernière intervention 21 novembre 2011
21 nov. 2011 à 18:03
J'ai enfin réussi. C'était simplement une erreur de syntaxe, Je marquais SourceData:= _"i" au lieu de _i

Merci

Private Sub ListeBPC_Click()

'TDC variable

'Definir la valeur de la variable i
i = Sheets("Data").Range("c14").Value
'Définir l'entete du pivotfield
j = Range("A1").Value

'Activer parade anti-scintillement
Application.ScreenUpdating = False
'supprimer l'ancien TCD
Columns("L:N").Select
Selection.Delete Shift:=xlToLeft

'créer le nouveau TCD
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
i, Version:= _
xlPivotTableVersion12).CreatePivotTable TableDestination:="BPC!R3C12", _
TableName:="PivotTable4", DefaultVersion:=xlPivotTableVersion12
'Mise en forme du TCD
Sheets("BPC").Select
Cells(3, 12).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("PivotTable4").PivotFields(j)
.Orientation = xlRowField
.Position = 1
End With
ActiveWorkbook.ShowPivotTableFieldList = False

'Masque le TCD
Columns("L:L").Select
Selection.EntireColumn.Hidden = True

'Définir la colonne du TCD comme source pour la liste de validation en A3
Range("A3").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$L$4:$L$1000"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With

'Filtre dans le TCD
ActiveSheet.PivotTables("PivotTable4").PivotFields(j).PivotFilters.Add _
Type:=xlCaptionDoesNotBeginWith, Value1:="s"

'Fin de parade anti- scintillement
Application.ScreenUpdating = True
End Sub
3
Rejoignez-nous