I'm fairly new to VBA so dont know a lot of the basics.
I created a pivot table and recorded my doing so. This is just a simplified one that I could put up here, the one I actually need is a bit more complicated and would generate a lot more code.
Anyway so the problem I'm having is that apparently pivot tables have names (like PivotTable1, PivotTable2 etc). So with the code I generated it says PivotTable1, and when I create a new table it's called 2. When I run the code it looks for PT1 and then stops. What's my way around this?
Here's the code. Problematic parts bolded.
I created a pivot table and recorded my doing so. This is just a simplified one that I could put up here, the one I actually need is a bit more complicated and would generate a lot more code.
Anyway so the problem I'm having is that apparently pivot tables have names (like PivotTable1, PivotTable2 etc). So with the code I generated it says PivotTable1, and when I create a new table it's called 2. When I run the code it looks for PT1 and then stops. What's my way around this?
Here's the code. Problematic parts bolded.
Sub Macro1()
'
' Macro1 Macro
'
'
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Name")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Gender")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Score"), "Sum of Score", xlSum
End Sub