I can't find the root cause of my runtime error 424. I know it's to do with a missing object, but I'm not sure where or which object that would even be in this case. My assumption is has to do with ActiveSheet but I'm a bit lost.
Sub Macro1()
'
' Macro1 Macro
'
'
Sheets.Add
Error begins here
PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
ActiveSheet.Range("A1").CurrentRegion.Select, Version:= _
xlPivotTableVersion12).CreatePivotTable TableDestination:="Sheet1!R3C1", _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion12
Error end here
Sheets("Sheet1").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Source Type")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Category")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("Category").Orientation = _
xlHidden
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Activity")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("USD Amount"), "Sum of USD Amount", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Quantity"), "Sum of Quantity", xlSum
End Sub
It is the problem with recorded Macros, that there is no Objects defined at all.
I would suggest to replace all "Active" phrases with variables. Here is the start:
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = wb.Sheets(1)
Set ws2 = wb.Sheets(2)
Dim Caches As PivotCache
Set Caches = wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
ws2.Range("A1:C3"), Version:= _
xlPivotTableVersion12)
I rarely need to create pivot tables programatically so, to get an understanding of what's going on I created a little sub to step through:
Sub CreatePivotOnNewSheet() Sheets.Add ActiveSheet.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ ActiveSheet.Range("A1").CurrentRegion, Version:= _ xlPivotTableVersion12).CreatePivotTable TableDestination:="Sheet1!R3C1", _ TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion12 End Sub
This compiles OK but throws an error 438: Object doesn't support this property or method. I searched the Object Browser for PivotCaches and found that it is a member of Workbook and PivotTable. It isn't a member of Worksheets! This makes sense when you consider that pivot tables on different sheets can use the same pivot cache.
So, change the code:
Sub CreatePivotOnNewSheet()
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
ActiveSheet.Range("A1").CurrentRegion, Version:= _
xlPivotTableVersion12).CreatePivotTable TableDestination:="Sheet1!R3C1", _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion12
End Sub
and now we get a run-time error:
Which needs to be bigger to display the full reason (thanks Microsoft!) but I guess it goes on to say reference to a range that contains at least two rows and has no blank cells in its top row. While in break mode I went to the newly added sheet and created a little 2x2 data table. Back in the IDE, pressed F8 and it worked although the pivot table didn't have any rows/columns etc. defined. I haven't attempted to test your remaining code which seems to create the definitions - I'll leave that to you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.