PivotCaches.Create 方法 (Excel)

创建新的 PivotCache。

语法

表达式创建 (SourceTypeSourceDataVersion)

表达 一个代表 PivotCaches 对象的变量。

参数

名称 必需/可选 数据类型 说明
SourceType 必需 XlPivotTableSourceType SourceType 可以是以下 XlPivotTableSourceType 常量之一: xlConsolidationxlDatabasexlExternal
SourceData 可选 Variant 新数据透视表缓存的数据。
版本 可选 Variant 数据透视表的版本。 Version 可以是 XlPivotTableVersionList 常量之一。

返回值

PivotCache

备注

使用此方法创建 PivotCache 时,不支持以下两个 XlPivotTableSourceType 常量: xlPivotTablexlScenario。 如果提供这两个常量之一,将返回运行时错误。

如果 SourceType 不是 xlExternal,则需要 SourceData 参数。 当 SourceTypexlConsolidationxlDatabase) 或当 SourceType 为 xlExternal) 时,应 (WorkbookConnection 对象传递 Range 对象 (。

传递 Range 对象时,建议使用字符串指定工作簿、工作表和单元格区域,或者设置命名区域并将名称作为字符串传递。 传递 Range 对象可能会意外导致“类型不匹配”错误。

如果不提供数据透视表的版本,则版本默认为 xlPivotTableVersion12。 不允许使用 xlPivotTableVersionCurrent 常量,如果提供该常量,将返回运行时错误。

示例

下面的代码示例定义连接,然后创建到 PivotCache 的连接

Workbooks("Book1").Connections.Add2 _
    "Target Connection Name", "", Array("OLEDB;Provider=MSOLAP.5;Integrated Security=SSPI;Persist Security Info=True;Data Source=##TargetServer##;Initial Catalog=Adventure Works DW", ""), 
    "Adventure Works", 1
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:=ActiveWorkbook.Connections("Target Connection Name"), _ 
    Version:=xlPivotTableVersion15).CreatePivotChart(ChartDestination:="Sheet1").Select

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。