简体   繁体   English

VB.NET数据透视表更改数据源

[英]VB.NET Pivot Table Change Data Source

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. 我一切正常,除非我尝试以编程方式更改VB.NET中数据透视表的数据源,但我收到了大量的COM异常未处理错误。 I am basing my code off of this SO 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): 当我在VBA中使用宏记录器执行此操作时,它可以正常工作并给出以下代码(请注意,我尝试使用命名范围,因此“ range1”是指我感兴趣的区域。我也尝试了不使用命名范围的情况) :

    ActiveSheet.PivotTables("PivotTable3").ChangePivotCache ActiveWorkbook. _
    PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Range1", Version _
    :=xlPivotTableVersion12)

This seems to line up well with what the SO post had suggested. 这似乎与SO职位的建议很一致。 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. 我已经尝试为数据透视表源类型进行外部,数据库和合并,以及使用命名范围,无命名范围,在SourceData中定义范围等。

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: 现在我知道,如有疑问,最好花一些时间在MSDN上,我碰到了ChangePivotCache页面,该页面显示:

The ChangePivotCache(Object) method can only be used with a PivotTable that uses data stored on a worksheet as its data source. ChangePivotCache(Object)方法只能与将存储在工作表上的数据用作其数据源的数据透视表一起使用。 A run-time error will occur if the ChangePivotCache(Object) method is used with a PivotTable that is connected to an external data source. 如果将ChangePivotCache(Object)方法与连接到外部数据源的数据透视表一起使用,则会发生运行时错误。

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. 所有这些中最令人沮丧的部分是我从宏记录器中发布的VBA代码工作正常且直观,因此对于VB.NET为何要让我的一天变得如此艰难,我有些茫然。

Any help would be much appreciated and as always, thanks a ton SO 任何帮助将不胜感激,并且一如既往,非常感谢

Well SO 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). 好吧,除了我引用原始工作簿数据透视表而不是复制的数据透视表(我称为表格,另一个称为tblsheet,所以这节课要注意并更好地命名变量)之外,我一切都很好。 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. 我要为仍然在VB.NET中自动化excel的任何人保留它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM