I am trying to have a macro that will create a pivot table on a sheet using the data from column C and D. I would like for this work on any sheet in the workbook and to run on the active sheet when the macro is run. When I make the pivot table I have no problem and it looks just like I want but when I run the macro it immediately has an error. Any help would be great.
Sub DailyTotals()
'
' DailyTotals Macro
'
' Keyboard Shortcut: Ctrl+Shift+T
'
Columns("C:D").Select
Range("D1").Activate
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"11-21!R1C3:R1048576C4", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="11-21!R3C11", TableName:="PivotTable5", DefaultVersion _
:=xlPivotTableVersion14
Sheets("11-21").Select
Cells(3, 11).Select
With ActiveSheet.PivotTables("PivotTable5").PivotFields("ITEM")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable5").AddDataField ActiveSheet.PivotTables( _
"PivotTable5").PivotFields("#"), "Count of #", xlCount
Range("L3").Select
With ActiveSheet.PivotTables("PivotTable5").PivotFields("Count of #")
.Caption = "Sum of #"
.Function = xlSum
End With
Range("K3").Select
With ActiveSheet.PivotTables("PivotTable5").PivotFields("ITEM")
.PivotItems("TIME").Visible = False
.PivotItems("UPS").Visible = False
.PivotItems("(blank)").Visible = False
End With
Range("K3").Select
ActiveSheet.PivotTables("PivotTable5").PivotFields("ITEM").AutoSort xlAscending _
, "ITEM"
ActiveWorkbook.ShowPivotTableFieldList = False
End Sub
Sub DailyTotals()
'
' DailyTotals Macro
'
' Keyboard Shortcut: Ctrl+Shift+T
'
Columns("C:D").Select
Range("D1").Activate
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"11-21!R1C3:R1048576C4", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="11-21!R3C11", TableName:="PivotTable5", DefaultVersion _
:=xlPivotTableVersion14
Sheets("11-21").Select
Cells(3, 11).Select
With ActiveSheet.PivotTables("PivotTable5").PivotFields("ITEM")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable5").AddDataField ActiveSheet.PivotTables( _
"PivotTable5").PivotFields("#"), "Count of #", xlCount
Range("L3").Select
With ActiveSheet.PivotTables("PivotTable5").PivotFields("Count of #")
.Caption = "Sum of #"
.Function = xlSum
End With
Range("K3").Select
With ActiveSheet.PivotTables("PivotTable5").PivotFields("ITEM")
.PivotItems("TIME").Visible = False
.PivotItems("UPS").Visible = False
.PivotItems("(blank)").Visible = False
End With
Range("K3").Select
ActiveSheet.PivotTables("PivotTable5").PivotFields("ITEM").AutoSort xlAscending _
, "ITEM"
ActiveWorkbook.ShowPivotTableFieldList = False
End Sub