vba:创建数据透视表

vba:创建数据透视表,vba,excel,pivot-table,Vba,Excel,Pivot Table,我必须使用vba创建数据透视表,但出现以下错误:“运行时错误'438'对象不支持此属性或方法”关于此代码:ActiveWorkbook.PivotCaches.create(SourceType:=xlDatabase,SourceData:=_ “Sheet1!R1C1:R1048576C8”,版本:=6)。CreatePivotTableDestination:=_ 数据透视表!R1C1,tableName:=tableName,DefaultVersion:=6 这里是完整的来源 Dim

我必须使用vba创建数据透视表,但出现以下错误:“运行时错误'438'对象不支持此属性或方法”关于此代码:
ActiveWorkbook.PivotCaches.create(SourceType:=xlDatabase,SourceData:=_
“Sheet1!R1C1:R1048576C8”,版本:=6)。CreatePivotTableDestination:=_
数据透视表!R1C1,tableName:=tableName,DefaultVersion:=6

这里是完整的来源

Dim tableName As String
Dim pivotTableWs As Worksheet

tableName = "pivotTableName"

Set pivotTableWs = Sheets.Add(after:=Worksheets("Sheet1"))
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "Sheet1!R1C1:R1048576C8", Version:=6).CreatePivotTable TableDestination:= _
    pivotTableWs!R1C1, tableName:=tableName, DefaultVersion:=6
Sheets(pivotTableWs).Select
Cells(1, 1).Select
With ActiveSheet.PivotTables(tableName)
    .ColumnGrand = True
    .HasAutoFormat = True
    .DisplayErrorString = False
    .DisplayNullString = True
    .EnableDrilldown = True
    .ErrorString = ""
    .MergeLabels = False
    .NullString = ""
    .PageFieldOrder = 2
    .PageFieldWrapCount = 0
    .PreserveFormatting = True
    .RowGrand = True
    .SaveData = True
    .PrintTitles = False
    .RepeatItemsOnEachPrintedPage = True
    .TotalsAnnotation = False
    .CompactRowIndent = 1
    .InGridDropZones = False
    .DisplayFieldCaptions = True
    .DisplayMemberPropertyTooltips = False
    .DisplayContextTooltips = True
    .ShowDrillIndicators = True
    .PrintDrillIndicators = False
    .AllowMultipleFilters = False
    .SortUsingCustomLists = True
    .FieldListSortAscending = False
    .ShowValuesRow = False
    .CalculatedMembersInFilters = False
    .RowAxisLayout xlCompactRow
End With
With ActiveSheet.PivotTables(tableName).PivotCache
    .RefreshOnFileOpen = False
    .MissingItemsLimit = xlMissingItemsDefault
End With
ActiveSheet.PivotTables(tableName).RepeatAllLabels xlRepeatLabels
With ActiveSheet.PivotTables(tableName).PivotFields("field1")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables(tableName).AddDataField ActiveSheet.PivotTables( _
    tableName).PivotFields("ticketid"), "Count of field1", xlCount
With ActiveSheet.PivotTables(tableName).PivotFields("field2")
    .Orientation = xlColumnField
    .Position = 1
End With

我使用“开发人员”选项卡创建此代码,选择“宏寄存器”,并手动创建数据透视表

我添加了两个对象变量
PvtTbl作为数据透视表
PvtCache作为数据透视缓存
,以使代码更具动态性

其他解释在下面的代码中(作为注释)

代码


我添加了两个对象变量
PvtTbl作为数据透视表
PvtCache作为数据透视缓存
,使代码更具动态性

其他解释在下面的代码中(作为注释)

代码


我也需要一个循环。这是我使用的(带有注释)。它应该适用于任何数据集

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

我也需要一个循环。这是我使用的(带有注释)。它应该适用于任何数据集

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

将TableDestination参数更改为
pivotTableWs.Cells(1,1)
将TableDestination参数更改为
pivotTableWs.Cells(1,1)