Currently I have a macro which successfully creates a pivot table which I wrote via the macro recorder. The problem I have run into occurs when I try to change the headings of two columns. Below is a copy of the relevant portion of the macro:
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Feb-15"), "Count of Feb-15", xlCount
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Mar-15"), "Count of Mar-15", xlCount
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Count of Feb-15")
.Caption = "Sum of Feb-15"
.Function = xlSum
.NumberFormat = "_(* #,##0.00_);[Red]_(* (#,##0.00);_(* ""-""??_);_(@_)"
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Count of Mar-15")
.Caption = "Sum of Mar-15"
.Function = xlSum
.NumberFormat = "_(* #,##0.00_);[Red]_(* (#,##0.00);_(* ""-""??_);_(@_)"
End With
ActiveSheet.PivotTables("PivotTable3").CalculatedFields.Add "Variance", _
"='Mar-15' -'Feb-15'", True
ActiveSheet.PivotTables("PivotTable3").PivotFields("Variance").Orientation = _
xlDataField
PivotFields Feb-15 or Mar-15 can change to be Q1 and Q2, or May-15 and Jun-15 for example, but they will always be in the same columns in the spreadsheet (J & K).
Is there a way to replace the PivotField reference with a generic column reference to achieve the same result ?
Many thanks in advance
Bookmarks