OLAP查询后运行宏

时间:2015-11-20 09:40:29

标签: excel vba excel-vba pivot-table olap

这是我的工作簿:

表格布局:

  1. Source:带有Analysis Services连接的数据透视表
  2. Distributors:与Pivot
  3. 连接的东西
  4. Output:基于与{Pivot相连的Distributors +切片器的排序数据的图表
  5. 我需要做的是:在每次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
    

1 个答案:

答案 0 :(得分:1)

解决。 在每次数据透视表更新后启动宏

  1. Alt + F11,右键单击Source(Sheet with PivotTable)
  2. 查看代码
  3. 插入事件触发器
  4. 事件触发器

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

    请记住,您无法输入对模块的引用。直接插入您的宏。

相关问题