Ok, so I have spent way too much time trying to figure this one out, so I'm turning to the experts! I am trying to create a macro that will take a selection of data, name it "Data", then use that selection as the source for my pivot table. Here is my code and I receive a Run-time 424: Object Required error at the bolded section...Can someone please tell me what I'm missing? Thanks in advance!
Sub TestPivot2()
'
' TestPivot2 Macro
'
'
Sheets("assetaccts").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Name = "Data"
Set PtCache = ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlDatabase, _
SourceData:=s.Range("Data"))
Set Pt = PtCache.CreatePivotTable( _
TableDestination:=Sheets("Pivot Sheet1").Range("A1"), _
TableName:="AM221 Data")
With ActiveSheet.PivotTables("AM221 Data").PivotFields(" ACCT UNIT")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("AM221 Data").PivotFields(" ACCT DESCRIPTION")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("AM221 Data").AddDataField ActiveSheet.PivotTables( _
"AM221 Data").PivotFields(" CURRENT YTD"), "Sum of CURRENT YTD", xlSum
With ActiveSheet.PivotTables("AM221 Data").PivotFields("Sum of CURRENT YTD")
.NumberFormat = "#,##0.00_);[Red](#,##0.00)"
End With
Range("A4").Select
With ActiveSheet.PivotTables("AM221 Data")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
ActiveSheet.PivotTables("AM221 Data").PivotFields(" ACCT UNIT").Subtotals _
= Array(False, False, False, False, False, False, False, False, False, False, False, False _
)
Columns("A:A").EntireColumn.AutoFit
ActiveSheet.PivotTables("AM221 Data").PivotFields(" ACCT UNIT"). _
RepeatLabels = True
End Sub
Bookmarks