从数据透视表缓存重新创建源数据
我正在尝试从使用数据透视表缓存的数据透视表中提取源数据并将其放入空白电子表格中。我尝试了以下操作,但它返回应用程序定义或对象定义的错误。
ThisWorkbook.Sheets.Add.Cells(1,1).CopyFromRecordset ThisWorkbook.PivotCaches(1).Recordset
文档 表明 PivotCache.Recordset 是 ADO 类型,因此这应该可以工作。我确实在参考文献中启用了 ADO 库。
关于如何实现这一目标有什么建议吗?
I am trying to extract the source data from a PivotTable that uses a PivotTable cache and place it into a blank spreadsheet. I tried the following but it returns an application-defined or object defined error.
ThisWorkbook.Sheets.Add.Cells(1,1).CopyFromRecordset ThisWorkbook.PivotCaches(1).Recordset
Documentation indicates that PivotCache.Recordset is an ADO type, so this ought to work. I do have the ADO library enabled in references.
Any suggestions on how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,似乎没有办法在 Excel 中直接操作 PivotCache。
我确实找到了解决方法。以下代码提取工作簿中找到的每个数据透视表的数据透视缓存,将其放入新的数据透视表中,并仅创建一个数据透视表字段(以确保数据透视缓存中的所有行都纳入总数中),然后触发ShowDetail,它创建一个包含所有数据透视表数据的新工作表。
我仍然想找到一种直接使用 PivotCache 的方法,但这可以完成工作。
Unfortunately, there appears to be no way to directly manipulate PivotCache in Excel.
I did find a work around. The following code extracts the the pivot cache for every pivot table found in a workbook, puts it into a new pivot table and creates only one pivot field (to ensure that all rows from the pivot cache are incorporated in the total), and then fires ShowDetail, which creates a new sheet with all of the pivot table's data in.
I would still like to find a way to work directly with PivotCache but this gets the job done.
转到“立即窗口”并键入
?thisworkbook.PivotCaches(1).QueryType
如果您得到 7 (xlADORecordset) 以外的内容,则 Recordset 属性不适用于此类型的 PivotCache,并将返回该错误。
如果您在该行上收到错误,则说明您的 PivotCache 根本不基于外部数据。
如果您的源数据来自 ThisWorkbook(即 Excel 数据),那么您可以使用
?thisworkbook.PivotCaches(1).SourceData
来创建范围对象并循环遍历它。
如果您的 QueryType 为 1 (xlODBCQuery),则 SourceData 将包含您要创建的连接字符串和命令文本以及 ADO 记录集,如下所示:
您需要 ADO 引用,但您说您已经有了该集。
Go to the Immediate Window and type
?thisworkbook.PivotCaches(1).QueryType
If you get something other than 7 (xlADORecordset), then the Recordset property does not apply to this type of PivotCache and will return that error.
If you get an error on that line, then your PivotCache is not based on external data at all.
If your source data comes from ThisWorkbook (i.e. Excel data), then you can use
?thisworkbook.PivotCaches(1).SourceData
To create a range object and loop through it.
If your QueryType is 1 (xlODBCQuery), then SourceData will contain the connection string and commandtext for you to create and ADO recordset, like this:
You need the ADO reference, but you said you already have that set.
我发现自己遇到了同样的问题,需要以编程方式使用缓存的数据透视数据来抓取来自不同 Excel 的数据。
虽然这个主题有点老了,但看起来仍然没有直接的方法来访问数据。
您可以在下面找到我的代码,它是对已发布的解决方案的更通用的改进。
主要区别是从字段中删除过滤器,因为有时数据透视会打开过滤器,如果您调用 .Showdetail 它将错过过滤后的数据。
我用它来抓取不同的文件格式,而无需打开它们,到目前为止它对我来说非常有用。
希望它有用。
归功于spreadsheetguru.com的过滤器清洁程序(虽然我不记得有多少是原创的,有多少是我的,老实说)
I found myself having the same problem, needing to scrape programmatically data coming different Excels with cached Pivot data.
Although the topic is a bit old, still looks there is no direct way to access the data.
Below you can find my code, which is a more generalized refinement of the already-posted solution.
The major difference is the filter removal from fields, as sometimes pivot comes with filters on, and if you call .Showdetail it will miss filtered data.
I use it to scrape from different file format without having to open them, it is serving me quite well thus far.
Hope it is useful.
Credit to spreadsheetguru.com on the filter cleaning routine (although I don't remember how much is original and how much is mine to be honest)