Hi. I created a macro recording to create a pivot table and, because it worked well on my test data set, used it in a functional spreadsheet.
Now I discover that the range for the pivot table data is hard coded, not what I wanted as the range is different every time it’s run.
Could you advise me on the code I could take out and what I could include in the recorded macro below please.
Many, many thanks.
Peter
Sub PTCode()
‘
‘ PTCode Macro
‘
‘
Range(“E1”).Select
ActiveCell.FormulaR1C1 = “1”
Range(“E1”).Select
Selection.Copy
Range(“D1”).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
SkipBlanks:=False, Transpose:=False
Range(“E1”).Select
Application.CutCopyMode = False
Selection.ClearContents
Range(“A1”).Select
Sheets.Add
‘this is where the range is hard coded
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
“Sheet1!R1C1:R455C4″, Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:=”Sheet2!R3C1″, TableName:=”PivotTable1”, DefaultVersion _
:=xlPivotTableVersion14
Sheets(“Sheet2”).Select
Cells(3, 1).Select
With ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Strand”)
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Name”)
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Resultset”)
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables(“PivotTable1”).AddDataField ActiveSheet.PivotTables( _
“PivotTable1”).PivotFields(“Result”), “Sum of Result”, xlSum
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Name”).Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Resultset”).Subtotals = _
Array(False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Strand”).Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Result”).Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)
With ActiveSheet.PivotTables(“PivotTable1”)
.ColumnGrand = False
.RowGrand = False
End With
ActiveSheet.PivotTables(“PivotTable1″).ShowPages PageField:=”Strand”
End Sub