[英]VBA to Create PivotTable, Type Mismatch? What Am I Doing Wrong?
Getting Type Mismatch on the line tabledestination:=("pivot!A3")
在行
tabledestination:=("pivot!A3")
上获取类型不匹配tabledestination:=("pivot!A3")
I want to add a sheet and name it "Pivot" and create PivotTable on that sheet. 我想添加一个工作表并将其命名为“数据透视表”,并在该工作表上创建数据透视表。
Dim pt As PivotTable
Dim Pcache As PivotCache
Sheets.add.name = "Pivot"
Sheets("DATA").Select
Set Pcache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Cells(1, 1).CurrentRegion)
Set pt = ActiveSheet.PivotTables.Add(PivotCache, tabledestination:=("pivot!A3"))
With pt
PivotFields.Subtotals(1) = False
.InGridDropZones = True
.RowAxisLayout xlTabularRow
.PivotFields("Apple").Orientation = xlRowField
.PivotFields("Apple Qty").Orientation = xlDataField
End With
This worked for me... 这对我有用...
Sub Sample()
Dim pt As PivotTable
Sheets.Add.Name = "Pivot"
With ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=Sheets("DATA").Cells(1, 1).CurrentRegion)
Set pt = .CreatePivotTable(TableDestination:="Pivot!R3C1")
End With
'~~> Rest of Code
End Sub
Or if you want to do it your way then 或者,如果您想按照自己的方式做,
Sub Sample()
Dim pt As PivotTable
Dim Pcache As PivotCache
Sheets.Add.Name = "Pivot"
Set Pcache = ActiveWorkbook.PivotCaches.Create(xlDatabase, _
Sheets("DATA").Cells(1, 1).CurrentRegion)
Set pt = Pcache.CreatePivotTable(tabledestination:=("Pivot!R3C1"))
'~~> Rest of the code
End Sub
Caution: If the sheet PIVOT exists you will get an error. 注意:如果存在PIVOT表,则会出现错误。 Maybe you would like to add this to your code?
也许您想将此添加到您的代码中?
Application.DisplayAlerts = False
On Error Resume Next
Sheets("Pivot").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Sheets.Add.Name = "Pivot"
Instead of using 而不是使用
Set pt = ActiveSheet.PivotTables.Add(PivotCache, tabledestination:=("pivot!A3"))
I used this to move forward: 我用这个来前进:
Sheets("Pivot").Activate
Set pt = ActiveSheet.PivotTables.Add(PivotCache:=Pcache, TableDestination:=Range("A3"))
Notice the addition of Sheets("Pivot").Activate
and PivotCache:=Pcache
. 请注意增加了
Sheets("Pivot").Activate
和PivotCache:=Pcache
。 I haven't checked the validity of code below that statement, though. 不过,我尚未检查该语句下代码的有效性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.