Reputation: 1057
I am trying to write a program that takes a file with a pivot table and a data sheet with aggregated information, copies those two tabs to a new workbook, deletes out some of the information from the aggregate information and then refreshes the pivot table with the new data.
I have everything working just fine, except when I try and programatically change the data source for the pivot table in VB.NET I get a whole host of COM exception unhandled errors. I am basing my code off of this S.O. post.
When I do it in VBA with the macro recorder it works fine and gives the following code (note I tried using named ranges, so "range1" here refers to the area I am interested in. I have tried it without named ranges as well):
ActiveSheet.PivotTables("PivotTable3").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Range1", Version _
:=xlPivotTableVersion12)
This seems to line up well with what the S.O. post had suggested. Here is the code I have most recently tried:
Dim pivotTbl As Excel.PivotTable = tableSheet.PivotTables("PivotTable3")
Dim holdRows As Integer = hold.UsedRange.Rows.Count
Dim holdCols As Integer = hold.UsedRange.Columns.Count
Dim pvtRng As Excel.Range = hold.Range("a1", hold.Range("a1").Offset(holdRows - 1, holdCols - 1))
savebook.Names.Add(Name:="Range1", RefersTo:=pvtRng)
pivotTbl.ChangePivotCache(savebook.PivotCaches.Create(SourceType:=Excel.XlPivotTableSourceType.xlDatabase, SourceData:="Range1"))
I have tried external, database and consolidated for my pivot table source types as well as using named ranges, no named ranges, having the range defined in SourceData, etc.
Now I know that when in doubt it is best to go spend some quality time on the MSDN and I came across the ChangePivotCache page, which says:
The ChangePivotCache(Object) method can only be used with a PivotTable that uses data stored on a worksheet as its data source. A run-time error will occur if the ChangePivotCache(Object) method is used with a PivotTable that is connected to an external data source.
So what I am left wondering is whether or not the fact that my new workbook has a link to the original workbook is part of the problem. I have gone ahead and tried to remove the link both programatically and by hand, but of course it is not disappearing.
The most frustrating part about all of it is that the VBA code that I posted from the macro recorder works just fine and is intuitive, so I am at a bit of a loss as to why VB.NET is trying to make my day so hard.
Any help would be much appreciated and as always, thanks a ton S.O.
Upvotes: 0
Views: 2589
Reputation: 1057
Well S.O. I had everything right except I was referencing the original workbook pivot table instead of the copied pivot table (one was called tablesheet and the other was called tblsheet, so a lesson in paying attention and better variable naming). So if you make sure your references are correct the above code should work like a dream. I am going to leave it up for the sake of anyone that is still out there automating excel in VB.NET.
Upvotes: 1