对透视表进行排序

  • 本文关键字:排序 透视 excel vba
  • 更新时间 :
  • 英文 :


我记录了这个宏来对数据透视表进行排序。

但是,数据透视表是动态的,只有当数据透视表在正确的列(k)结束时,这个宏才能工作。

是否有一种方法可以使宏影响最后一列中的数据?

On Error GoTo ErrorHandler
ErrorHandler:
Resume Next
Range("K4").Select
ActiveSheet.PivotTables("InventoryPivotTable").PivotFields("Products"). _
AutoSort xlAscending, "FBM ", ActiveSheet.PivotTables("InventoryPivotTable"). _
PivotColumnAxis.PivotLines(8), 1

ActiveWorkbook.ShowPivotTableFieldList = False
Call highlightNegativeNumbersFBM

你可能根本不需要"Select">

你也不需要知道它是按k列排序的,它实际上是按k列中的第8个枢轴行排序的

如果我重写这行

ActiveSheet.PivotTables("InventoryPivotTable").PivotFields("Products").       AutoSort xlAscending, "FBM ", ActiveSheet.PivotTables("InventoryPivotTable").        PivotColumnAxis.PivotLines(8), 1
dim lPivotTable as pivotTable
set lPivotTable = ActiveSheet.PivotTables("InventoryPivotTable") 
call LPivotTable.PivotFields("Products").AutoSort(xlAscending, "FBM ", lPivotTable.PivotColumnAxis.PivotLines(lPivotTable.PivotColumnAxis.PivotLines.Count), 1)

除了缩短它,使用变量,我用lPivotTable.PivotColumnAxis.PivotLines.Count代替了8,它应该等于8,直到你添加或删除项目

最新更新