【发布时间】:2020-11-10 21:42:19
【问题描述】:
我正在尝试使用以下代码创建数据透视,但是我收到错误,
“无效的过程调用或参数”
有人可以帮忙吗。
这是我的代码:
Sub Creatingquerypivot()
Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim SrcData As String
Dim Pf As PivotField
'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=Sheets("Queries").Name & "!" & Sheets("Queries").Range("A3").CurrentRegion.Address)
'Create a new worksheet
Set sht = Sheets.Add
sht.Name = "Queries Summary"
'Where do you want Pivot Table to start?
StartPvt = sht.Name & "!" & sht.Range("A3").Address(ReferenceStyle:=xlA1)
'Create Pivot table from Pivot Cache
- Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="QueriesSummary")
'To add fields to the pivot table
Set Pf = pvt.PivotFields("Folder")
Pf.Orientation = xlRowField
Set Pf = pvt.PivotFields("Category")
Pf.Orientation = xlRowField
Set Pf = pvt.PivotFields("Received")
Pf.Orientation = xlDataField
【问题讨论】:
-
我认为
TableDestination需要一个Range对象,而不是String。 -
用 Range 替换字符串也不行。
-
"Not working"不是一个有用的描述。你能edit你的问题与你正在使用的修改后的代码和错误信息吗? -
TableDestination需要一个 Variant - 不需要是 Range 对象,或者通过创建数据透视表记录的宏不起作用 - 它们总是使用“范围字符串”方法。
标签: excel vba runtime-error pivot-table