I get a Run-time error '1004': Unable to get the PivotTables property of the Worksheet class. The error is in the Create PivotTable2 on Overhead Sheet section of the code and I have attached a picture of the highlighted code with the issue. Any help is really appreciated.
VBA Code:
'Create PivotTable1 on Overhead Sheet
Sheets("Time Export").Select
Range("A1").Select
Selection.CurrentRegion.Select
ActiveWorkbook.Worksheets("Time Export").PivotTables("PivotTable1").PivotCache. _
CreatePivotTable TableDestination:="Overhead!R2C2", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion12
Sheets("Overhead").Select
Range("B2").Select
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Customer Hours/ Units"), _
"Sum of Customer Hours/ Units", xlSum
With ActiveSheet.PivotTables("PivotTable1")
.ColumnGrand = True
.HasAutoFormat = True
.DisplayErrorString = False
.DisplayNullString = True
.EnableDrilldown = True
.ErrorString = ""
.MergeLabels = False
.NullString = ""
.PageFieldOrder = 2
.PageFieldWrapCount = 0
.PreserveFormatting = True
.RowGrand = True
.SaveData = True
.PrintTitles = False
.RepeatItemsOnEachPrintedPage = True
.TotalsAnnotation = False
.CompactRowIndent = 1
.InGridDropZones = False
.DisplayFieldCaptions = True
.DisplayMemberPropertyTooltips = False
.DisplayContextTooltips = True
.ShowDrillIndicators = True
.PrintDrillIndicators = False
.AllowMultipleFilters = False
.SortUsingCustomLists = True
.FieldListSortAscending = False
.ShowValuesRow = False
.CalculatedMembersInFilters = False
.RowAxisLayout xlCompactRow
End With
With ActiveSheet.PivotTables("PivotTable1").PivotCache
.RefreshOnFileOpen = False
.MissingItemsLimit = xlMissingItemsDefault
End With
ActiveSheet.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Resource")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Project")
.Orientation = xlRowField
.Position = 2
End With
'Create PivotTable2 on Overhead Sheet
Sheets("Time Export").Select
Range("A1").Select
Selection.CurrentRegion.Select
ActiveWorkbook.Worksheets("Time Export").PivotTables("PivotTable2").PivotCache. _
CreatePivotTable TableDestination:="Overhead!R2C5", TableName:= _
"PivotTable2", DefaultVersion:=xlPivotTableVersion12
Sheets("Overhead").Select
Range("E2").Select
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Customer Hours/ Units"), _
"Sum of Customer Hours/ Units", xlSum
With ActiveSheet.PivotTables("PivotTable2")
.ColumnGrand = True
.HasAutoFormat = True
.DisplayErrorString = False
.DisplayNullString = True
.EnableDrilldown = True
.ErrorString = ""
.MergeLabels = False
.NullString = ""
.PageFieldOrder = 2
.PageFieldWrapCount = 0
.PreserveFormatting = True
.RowGrand = True
.SaveData = True
.PrintTitles = False
.RepeatItemsOnEachPrintedPage = True
.TotalsAnnotation = False
.CompactRowIndent = 1
.InGridDropZones = False
.DisplayFieldCaptions = True
.DisplayMemberPropertyTooltips = False
.DisplayContextTooltips = True
.ShowDrillIndicators = True
.PrintDrillIndicators = False
.AllowMultipleFilters = False
.SortUsingCustomLists = True
.FieldListSortAscending = False
.ShowValuesRow = False
.CalculatedMembersInFilters = False
.RowAxisLayout xlCompactRow
End With
With ActiveSheet.PivotTables("PivotTable2").PivotCache
.RefreshOnFileOpen = False
.MissingItemsLimit = xlMissingItemsDefault
End With
ActiveSheet.PivotTables("PivotTable2").RepeatAllLabels xlRepeatLabels
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Resource")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Project")
.Orientation = xlRowField
.Position = 2
End With