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

TCD champs calculs VBA

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...

Si vous voyez ou est le problème, surtout n'hésitez pas :-)

sub TCD()

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"Collecting!A:AD").CreatePivotTable TableDestination:="", TableName _
:="PivotTable2"

waggle =1
waggle = make_fields("Cost of sales", waggle, "#,##0.00", "PivotTable2")
waggle = make_fields("GM", waggle, "#,##0.00", "PivotTable2")
waggle = make_fields("gm in %", waggle, "0.00%", "PivotTable2")
waggle = make_fields("CPU", waggle, "#,##0.00", "PivotTable2")

....

With ActiveSheet.PivotTables("PivotTable2")
.PivotFields("Sum of Cost of sales").Orientation = xlHidden
'.PivotFields("Sum of GM").Orientation = xlHidden
'.PivotFields("Sum of GM in %").Orientation = xlHidden
'.PivotFields("Sum of CPU").Orientation = xlHidden
'Les 3 lignes plantent...Ce n'est pas le with, déjà testé lol
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

Merci d'avance à tous
Cordialement
Benjamin

1 réponse

Avatar
poypoy
Ooops j'ai oublié le champ NR ainsi que de calculer les champs calculés lol
bon on simplifie :
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
.PivotFields("Sum of GM").Orientation = xlHidden

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 ?
Cordialement
Benjamin


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...

Si vous voyez ou est le problème, surtout n'hésitez pas :-)

sub TCD()

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"Collecting!A:AD").CreatePivotTable TableDestination:="", TableName _
:="PivotTable2"

waggle =1
waggle = make_fields("Cost of sales", waggle, "#,##0.00", "PivotTable2")
waggle = make_fields("GM", waggle, "#,##0.00", "PivotTable2")
waggle = make_fields("gm in %", waggle, "0.00%", "PivotTable2")
waggle = make_fields("CPU", waggle, "#,##0.00", "PivotTable2")

....

With ActiveSheet.PivotTables("PivotTable2")
.PivotFields("Sum of Cost of sales").Orientation = xlHidden
'.PivotFields("Sum of GM").Orientation = xlHidden
'.PivotFields("Sum of GM in %").Orientation = xlHidden
'.PivotFields("Sum of CPU").Orientation = xlHidden
'Les 3 lignes plantent...Ce n'est pas le with, déjà testé lol
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

Merci d'avance à tous
Cordialement
Benjamin