这是我的工作簿:
Source
:带有Analysis Services连接的数据透视表Distributors
:与Pivot Output
:基于与{Pivot相连的Distributors
+切片器的排序数据的图表我需要做的是:在每次OLAP查询后启动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
答案 0 :(得分:1)
解决。 在每次数据透视表更新后启动宏
事件触发器
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
'your_macro_here
End Sub
请记住,您无法输入对模块的引用。直接插入您的宏。