lundi 20 avril 2015

Adding multiple PivotFields in Excel VBA

I'm trying to make a macro which makes a PivotTable. Currently I've been able to find some code that helped me create it, the problem is that I can't find a way to add various PivotFields using a for look or array, currently I use this code to add PivotFields:

'set data field - specifically change orientation to a data field and
'set its function property:
 With PvtTbl.PivotFields("S1")

.Orientation = xlDataField

.Function = xlSum

.NumberFormat = "#,##0"

.Position = 1

End With

With PvtTbl.PivotFields("S2")

.Orientation = xlDataField

.Function = xlSum

.NumberFormat = "#,##0"

.Position = 2

End With

As you can see i just repeat the same code to add another field, is there a better way for adding multiple fields instead of just copy paste the same code? I need to add 22 fields.

Aucun commentaire:

Enregistrer un commentaire