我正在尝试创建数据透视表,但它会抛出以下错误 - “运行时错误13 - 类型不匹配”。当我通过行运行代码时,错误落在最后一行的“activeworkbook.pivotcaches.etc ...”
Sub testmodule()
Dim wsA, wsB As Worksheet
Set wsA = ThisWorkbook.Sheets("Sheet1")
Set wsB = ThisWorkbook.Sheets("Pivot Tables")
Set rnge = wsA.Range("B6")
Set rngData = Range(rnge, rnge.End(xlToRight))
Set rngData = Range(rnge, rnge.End(xlDown))
Set rngB = wsB.Range("C8")
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, _
Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=rngB, _
TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14
End Sub
有人可以告诉我哪里出错吗?
答案 0 :(得分:1)
更改... PivotCaches.Create
...到... PivotCaches.Add
...
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=rngData, _
Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=rngB, _
TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14
Here's指向Microsoft Dev Center上PivotCaches对象的链接。它使用Add方法而不是Create(但你的语法是正确的。)