2017-09-27 232 views
0

数据透视表是使用数据连接的值创建的。对数据透视表中的值进行排序

我保存了一个宏来对值进行排序,但当我尝试在更新到另一个连接后运行它时,现在出现“下标超出范围”警告。我猜宏脚本正在使用我需要替换的静态范围,以便它可以运行到我可能具有的任何数据透视表结果。

但是我需要替换哪一行宏代码?

Sub SortLargestWarningsCount() 

'Find the last used column in a Row 
    Dim LastCol As Integer 
    With ActiveSheet 
     LastCol = .Cells(6, .Columns.Count).End(xlToLeft).Column 
    End With 

Worksheets("WARNINGS").Cells(6, LastCol).Activate 
ActiveCell.Offset(1, 0).Activate 

'Sort from Largest 
    ActiveSheet.PivotTables("WarningsPivotTable").PivotFields(_ 
    "[Warnings].[Column5].[Column5]").AutoSort xlDescending, _ 
    "[Measures].[Count of Column5]", ActiveSheet.PivotTables("WarningsPivotTable"). _ 
    PivotColumnAxis.PivotLines(12), 1 
End Sub 

谢谢你的帮忙。

回答

0

使用下面SortPivotTable根据 “警告” PivotField代码:

Dim PvtSht As Worksheet 
Dim PvtTbl As PivotTable 

' set the worksheet object where the Pivot-Table is locates 
Set PvtSht = Worksheets("WARNINGS") 

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

With PvtTbl 
    'Sort from Largest for PivotFiels "Warning" 
    .PivotFields("Warning").AutoSort xlDescending, "Warning" 
End With 
0

谢谢您的答复。

获取“无法获取数据透视表类的PivotFields属性”错误。

是不是因为我需要用列/字段替换下面的“警告”我想对它进行排序?如果是这样,我想排序的列/字段是大总计。

PivotFields(“Warning”)。AutoSort xlDescending,“Warning”