OLAP查询后运行macros

这是我的工作簿:

表格布局:

  1. Source :带有Analysis Services连接的数据透视表
  2. Distributors :与Pivot连接的东西
  3. Output :基于Distributors +连接到数据透视的切片器的sorting数据的图表

我需要做的是: 在每个OLAP查询(每次使用切片器)后启动Sortingmacros。

sorting代码

 Sub Sorting() 'This line finds the last occupied row in column A 'And you can use that LR variable in all the following Range Statements. LR = Cells(Rows.Count, "C").End(xlUp).Row ActiveWorkbook.Worksheets("Distributors").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Distributors").Sort.SortFields.Add Key:=Range("C4:C102" & LR) _ , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Distributors").Sort .SetRange Range("A3:C102" & LR) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With ActiveWorkbook.Worksheets("Distributors").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Distributors").Sort.SortFields.Add Key:=Range("D4:D102") _ , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Distributors").Sort .SetRange Range("D3:F102") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub 

解决了。 在每个数据透视表更新后启动macros

  1. Alt + F11,右键单击源(带有数据透视表的工作表)
  2. 查看代码
  3. 插入事件触发器

事件触发器

 Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable) 'your_macro_here End Sub 

请记住,您无法input对模块的引用。 直接插入你的macros。