在数据透视表中对值进行排序

时间:2017-09-27 07:54:17

标签: excel-vba vba excel

使用数据连接中的值创建数据透视表。

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

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

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

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

根据"警告&#34>,使用以下代码Sort PivotTable 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

apply plugin: 'kotlin-android-extensions'

答案 1 :(得分:0)

感谢您的回复。

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

是不是因为我需要更换"警告"我希望对列/字段进行排序吗?如果是这样,我要排序的列/字段是Grand Totals。

PivotFields("警告")。AutoSort xlDescending,"警告"