我必须使用vba创建一个数据透视表,但是我收到以下错误:“运行时错误'438'对象不支持此属性或方法”关于此代码:MySqlCommand
这里是完整的来源
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R1048576C8", Version:=6).CreatePivotTable TableDestination:= _
pivotTableWs!R1C1, tableName:=tableName, DefaultVersion:=6
我使用“Developer”选项卡创建此代码,选择“Macro register”并手动创建数据透视表
答案 0 :(得分:2)
我添加了2个对象变量y
和PvtTbl As PivotTable
,以使代码更具动态性。
其他解释在下面的代码中(作为评论)。
<强> 代码 强>
PvtCache As PivotCache
答案 1 :(得分:0)
我同样需要循环。这就是我使用的(带有注释)。它应该适用于任何数据集。
Sub createPivot()
'declare Range variable
Dim dataRange As Range
'get last row and last column in the data sheet
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
'set value for dataRange; this is dynamic and will work for any dataset
Set dataRange = Range(Cells(1, 1), Cells(lastrow, lastcol))
dataRange.Select
'create new WS and insert blank pivot table
Sheets.Add
ActiveSheet.Name = "Pivot"
ActiveWorkbook.PivotCaches.Create(xlDatabase, dataRange, 6).CreatePivotTable Sheets("Pivot").Range("A3"), "PivotTable1", dataRange, 6
' the following 2 blocks are optional
'Insert Row Fields
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Rights Holder")
.Orientation = xlRowField
.Position = 1
End With
'Insert Values fields
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Number of tracks")
.Orientation = xlDataField
.Function = xlSum
End With
End Sub