Excel VBA Error 9 with PivotTables

saisis

New Member
Joined
Oct 21, 2014
Messages
9
Hello, I recorded a macro to create and setup a pivottable from Sheet "Tags" to Sheet "Flt Hours". When I ran the recorded macro, I got a message saying Error 9: subscript out of range. The highlighted code is:
ActiveWorkbook.Worksheets("Flt Hrs").PivotTables("PivotTable1").PivotCache. _
CreatePivotTable TableDestination:="Flt Hrs!R3C6", TableName:="PivotTable1" _
, DefaultVersion:=xlPivotTableVersion14

The last line " , DefaultVersion:=xlPivotTableVersion14 " is where it says the error is occurring. Here is the rest of the code:
Sheets("Tags").Select
Range("A2").Select
ActiveWorkbook.Worksheets("Flt Hrs").PivotTables("PivotTable1").PivotCache. _
CreatePivotTable TableDestination:="Flt Hrs!R3C6", TableName:="PivotTable1" _
, DefaultVersion:=xlPivotTableVersion14
Sheets("Flt Hrs").Select
Cells(3, 6).Select
ActiveWorkbook.ShowPivotTableFieldList = True
'Sheets("Flt Hours").Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Primary Fault")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Partner")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Downtime Category")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Interval Hours"), "Sum of Interval Hours", xlSum

I am using Windows 7, Excel 2010. The code works to put in the fields for the PivotTable, but fails when the table is being created. I'm familiar with VBA, but frankly, I'm stumped. I've looked at several different posts, and nothing I've seen matches this problem.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
how many pivot tables exist, I had problems with more than one PivotTable1, my cure was to rename to different

do all your data have a header value, if i have problems thats where i start to look and add underscore inplace of blanks i.e Primary Fault / Primary_Fault
 
Upvote 0
Thank you mole999.



I've been working on trying to generalize the pivot table names so that it will work with any pivot tables, however the name is not the current issue as I have found out.

What I've been doing to test the macro is closing all open excel documents, then reopening the wb with the recorded macro and opening up a new wb for the macro to run on. This resets the counts for tables, charts, objects etc. for the new wb to "1". The bug happens on line "DefaultVersion:=xlPivotTableVersion14" when PT is being created.

If I remark out the creating PT code, create a PT manually, and let the macro run, it works fine and has no bugs. I wonder why I get the error with that one line since default version 14 is for Excel 2010, which is the Excel version I am using.

Also, if I only remark the line "DefaultVersion:=xlPivotTableVersion14" I get an error saying it has to be in the code.

I will try your suggestion to replace the spaces with underscores and post my results. Again, thank you!
 
Upvote 0
Thank you so much mole999! I finally got the opportunity to try your suggestion to replace the spaces with underscores. Just doing that did the trick! Now my macro is working flawlessly! Again, Thank You!!! :biggrin: I will keep this tip in mind next time I need to work on a macro to create a pivot table :)
 
Upvote 0
glad it sorted your issue
 
Upvote 0

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top