Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

[VB] grammaire d'un TCD avec affichage de certains champs

1 réponse
Avatar
jean-francois LEGRAS
Bonjour,


J'ai une macro qui fonctionne mais je voudrais savoir s'il y a une façon
peut-être plus propre de l'écrire...

Merci de vos suggestions.

Slts
JF


sub test()

Set MonTCD1 = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase,
SourceData:=[stats!A1:B2]).CreatePivotTable(TableDestination:=Sheets("onglet1").Cells(3,
1), TableName:="Tableau croisé dynamique",
DefaultVersion:=xlPivotTableVersion10)

With MonTCD1

.SourceData = [stats!A1].CurrentRegion.Address(, , xlR1C1, True)
.AddFields RowFields:="Livré"

With .PivotFields("Net"): .Orientation = xlDataField: .Position = 1:
.Function = xlSum: End With
With .PivotFields("Rubrique"): .Orientation = xlColumnField: .Position =
1: End With

End With


Dim p As PivotItem
With MonTCD1.PivotFields("Rubrique")
For Each p In .PivotItems
p.Visible = True
Next p
For Each p In .PivotItems
If p.Value = "016 PROD SCOLAIR/EDUCAT " Or p.Value = "017 LOISIRS
CREAT/JEUX " Then p.Visible = False
Next p
End With


End sub

1 réponse

Avatar
MichDenis
Bonjour François,

Une façon de faire...
C'est personnel, mais en scindant l'information c'est une manière
plus simple je crois pour la compréhension... et ça rend chaque
item plus accessible si une modification doit être apportée au code.

'------------------------------------------------------------
Sub Exemple()

'Déclaration de toutes les variables
Dim PtCache As PivotCache, Pt As PivotTable
Dim Pf As PivotField, Pi As PivotItem

With Worksheets("stats")
'Définir où sont les données pour le pivotcache
Adr = .Name & "!" & .Range("A1:P" & _
.Range("A65536").End(xlUp).Row).Address
End With

'Créer le PivotCache, à partir de ce dernier
'plusieurs pivottables peuvent être créés.
'C'est l'espace mémoire où sont organisées toutes
'les données servant aux différents pivottables
Set PtCache = ThisWorkbook.PivotCaches.Add( _
SourceType:=xlDatabase, SourceData:­r)

'Créer le PivotTable 'où il sera créé dans la feuille
'et le nom qu'il portera
Set Pt = PtCache.CreatePivotTable( _
TableDestination:=Range("Feuil1!A5"), _
TableName:="Denis")

'Variable -> pour ton PivotField
Set Pf = Pt.PivotFields("Rubrique")
'Boucle sur chaque item du champ
For Each Pi In Pf.PivotItems
If Pi.Value = "016 PROD SCOLAIR/EDUCAT " Or _
Pi.Value = "017 LOISIRS CREAT/JEUX " Then
Pi.Visible = False
Else
Pi.Visible = True
End With
Next
Set PtCache = Nothing: Set Pt = Nothing
Set Pf = Nothing: Set Pi = Nothing

End Sub
'------------------------------------------------------------




"jean-francois LEGRAS" a écrit dans le message de groupe de
discussion :
Bonjour,


J'ai une macro qui fonctionne mais je voudrais savoir s'il y a une façon
peut-être plus propre de l'écrire...

Merci de vos suggestions.

Slts
JF


sub test()

Set MonTCD1 = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase,
SourceData:=[stats!A1:B2]).CreatePivotTable(TableDestination:=Sheets("onglet1").Cells(3,
1), TableName:="Tableau croisé dynamique",
DefaultVersion:=xlPivotTableVersion10)

With MonTCD1

.SourceData = [stats!A1].CurrentRegion.Address(, , xlR1C1, True)
.AddFields RowFields:="Livré"

With .PivotFields("Net"): .Orientation = xlDataField: .Position = 1:
.Function = xlSum: End With
With .PivotFields("Rubrique"): .Orientation = xlColumnField: .Position 1: End With

End With


Dim p As PivotItem
With MonTCD1.PivotFields("Rubrique")
For Each p In .PivotItems
p.Visible = True
Next p
For Each p In .PivotItems
If p.Value = "016 PROD SCOLAIR/EDUCAT " Or p.Value = "017 LOISIRS
CREAT/JEUX " Then p.Visible = False
Next p
End With


End sub