OVH Cloud OVH Cloud

TCD 3eme chance ^^

1 réponse
Avatar
poypoy
Bonjour,

Je crée un TCD contenant des champs calculés avec VBA. Ensuite, je souhaite
changer une partie des pivotfields (notamment des champs calculés) et là,
cela plante.

Si ce n'est pas un champs calculé, un pivotfield.orientation =xlhidden
marche masi si c'en est un, impossible ! Pourtant si je demande à
l'enregistreur de macro, il me sort la même chose...

voila en sheet 1 j'ai
A B
1 NR COS
2 100 50
3 150 70

et le code suivant qui plante quand je veux retirer GM :

Sub essai()
Dim waggle As Integer

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"sheet1!A:B").CreatePivotTable TableDestination:="", TableName _
:="PivotTable2"
ActiveSheet.PivotTables("PivotTable2").CalculatedFields.Add "GM", _
"='NR'- 'COS'"

waggle = 1
waggle = make_fields("NR", waggle, "#,##0.00", "PivotTable2")
waggle = make_fields("COS", waggle, "#,##0.00", "PivotTable2")
waggle = make_fields("GM", waggle, "#,##0.00", "PivotTable2")

With ActiveSheet.PivotTables("PivotTable2")
.PivotFields("Sum of NR").Orientation = xlHidden
'la ca marche nickel
.PivotFields("Sum of GM").Orientation = xlHidden
'pouf cela ne veut plus
End With

End Sub
Function make_fields(field As String, position As Integer, style As String,
pivotname As String)
With ActiveSheet.PivotTables(pivotname).PivotFields(field)
.Orientation = xlDataField
.NumberFormat = style
.Function = xlSum
.position = position
End With
make_fields = position + 1

End Function

Comment peut on le retirer (autrement qu'à la main :-) ?
Cordialement
Benjamin

1 réponse

Avatar
isabelle
bonjour poypoy,

malheureusement la propriété Visible = False s'applique seulement aux PivotItems
avec cette ligne,

.PivotFields("Sum of NR").Orientation = xlHidden

tu enlève ce champ du tcd, tu ne peux pas l'enlever une deuxième fois puisqu'il n'est plus là,
si tu veux le remettre il faut ajouter cette ligne

With ActiveSheet.PivotTables("PivotTable2")
.PivotFields("Somme de NR").Orientation = xlHidden
waggle = make_fields("NR", waggle, "#,##0.00", "PivotTable2")
End With

il reste un problème au niveau de la position dans la function que tu peux résoudre avec,

x = ActiveSheet.PivotTables("PivotTable2").PivotFields.Count

isabelle


Bonjour,

Je crée un TCD contenant des champs calculés avec VBA. Ensuite, je souhaite
changer une partie des pivotfields (notamment des champs calculés) et là ,
cela plante.

Si ce n'est pas un champs calculé, un pivotfield.orientation =xlhidden
marche masi si c'en est un, impossible ! Pourtant si je demande Ã
l'enregistreur de macro, il me sort la même chose...

voila en sheet 1 j'ai
A B
1 NR COS
2 100 50
3 150 70

et le code suivant qui plante quand je veux retirer GM :

Sub essai()
Dim waggle As Integer

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"sheet1!A:B").CreatePivotTable TableDestination:="", TableName _
:="PivotTable2"
ActiveSheet.PivotTables("PivotTable2").CalculatedFields.Add "GM", _
"='NR'- 'COS'"

waggle = 1
waggle = make_fields("NR", waggle, "#,##0.00", "PivotTable2")
waggle = make_fields("COS", waggle, "#,##0.00", "PivotTable2")
waggle = make_fields("GM", waggle, "#,##0.00", "PivotTable2")

With ActiveSheet.PivotTables("PivotTable2")
.PivotFields("Sum of NR").Orientation = xlHidden
'la ca marche nickel
.PivotFields("Sum of GM").Orientation = xlHidden
'pouf cela ne veut plus
End With

End Sub
Function make_fields(field As String, position As Integer, style As String,
pivotname As String)
With ActiveSheet.PivotTables(pivotname).PivotFields(field)
.Orientation = xlDataField
.NumberFormat = style
.Function = xlSum
.position = position
End With
make_fields = position + 1

End Function

Comment peut on le retirer (autrement qu'Ã la main :-) ?
Cordialement
Benjamin