Vba 应用;“数据透视字段.自动排序”;生成透视图时

Vba 应用;“数据透视字段.自动排序”;生成透视图时,vba,pivot-chart,Vba,Pivot Chart,我正在创建一个宏来生成一些聚集的列透视图。我使用了internet/stack overflow中的示例来生成透视表,生成透视图,并按降序显示列 我查阅了微软的文档,试图修改PivotField.Autosort的语法 这是我的代码的一个通用版本,带有注释。按降序显示列的Autosort命令位于最后两行: 'Declare Variables Dim PSheet As Worksheet 'To create a sheet for a new pivot table. Dim DSheet

我正在创建一个宏来生成一些聚集的列透视图。我使用了internet/stack overflow中的示例来生成透视表,生成透视图,并按降序显示列

我查阅了微软的文档,试图修改
PivotField.Autosort
的语法

这是我的代码的一个通用版本,带有注释。按降序显示列的Autosort命令位于最后两行:

'Declare Variables
Dim PSheet As Worksheet 'To create a sheet for a new pivot table.
Dim DSheet As Worksheet 'To use as a data sheet.
Dim PCache As PivotCache 'To use as a name for pivot table cache.
Dim PTable As PivotTable 'To use as a name for our pivot table.
Dim PRange As Range 'to define source data range.
Dim LastRow As Long 'To get the last row and column of our data range.
Dim LastCol As Long '

Dim chtObj      As ChartObject
Dim PvtSht      As Worksheet
Dim PvtTbl      As PivotTable

'After inserting a new worksheet, this code will set the value of FSheet
'variable to pivot table worksheet and DSheet to source data worksheet.
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("MaintActivType").Delete
Worksheets("Sheet1").Select
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "MaintActivType"
Application.DisplayAlerts = True
Set PSheet = Worksheets("MaintActivType")
Set DSheet = Worksheets("Sheet1")
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

'Define Pivot Cache
'In Excel 2000 and above, before creating a pivot
'table you need to create a pivot cache to define the data source.
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="MaintenanceData")

'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="MaintenanceData")

'Insert Row Fields
With ActiveSheet.PivotTables("MaintenanceData").PivotFields("MaintActivType")
    .Orientation = xlRowField
    .Position = 1 'this field allows for sub-categories of data to be displayed
End With

'Insert Column Fields
With ActiveSheet.PivotTables("MaintenanceData").PivotFields("MaintActivType")
    .Orientation = xlDataField
    .Position = 1
    .Function = xlCount
    .Name = "Count of MaintActivType"
End With

' set the Pivot sheet
Set PvtSht = Sheets("MaintActivType")

' set the Pivot Table object
Set PvtTbl = PvtSht.PivotTables("MaintenanceData")

' set the Chart Object
Set chtObj = PvtSht.ChartObjects.Add(300, 200, 550, 200)

' modify ChartObject properties
With chtObj
    .Chart.SetSourceData PvtTbl.TableRange2 ' set the chart's data range to the Pivot-Table's TableRange2
    .Chart.ChartType = xlColumnClustered
    .Name = "Maintenance Activity Type"
    .Chart.HasTitle = True
    .Chart.ChartTitle.Text = "Maintenance Activity Type"
    .Chart.HasLegend = False
End With

ActiveSheet.PivotTables("MaintenanceData").PivotField("MaintActivType") _
  .AutoSort xlDescending, "Sum of MaintActivType"
'End With
我没有得到任何与Autosort命令相关的错误消息,但是如果我在error Resume Next上注释掉
,我会得到一个pivot缓存创建的类型不匹配错误(“SetPcache…”),这是令人费解的,因为该部分在错误消息被抑制的情况下工作正常。

“在错误消息被抑制的情况下,该部分工作正常”不工作正常,不工作也没关系

Dim PCache As PivotCache

'...
'...

Set PCache = ActiveWorkbook.PivotCaches.Create _
          (SourceType:=xlDatabase, SourceData:=PRange). _
          CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
          TableName:="MaintenanceData")
在这里,您创建一个数据透视缓存,然后使用它创建一个数据透视表,并尝试将该数据透视表分配给您的PCache变量(该变量只能用于数据透视缓存…),这样会引发一个运行时错误,但此时数据透视表已创建,您将在下一行获取该错误


感谢您的回复,我查看了您之前的示例,这消除了一些错误。我用所附代码按照我想要的方式对图形进行了排序;有更好的方法吗?(对于糟糕的格式表示抱歉)`with PvtSht.Range(“B2”),.Cells(.Rows.Count,“B”).End(xlUp)).Sort\ukey1:=.Range(“B2”),Order1:=xlDescending,uheader:=xlYes以``结尾,我觉得很好