Run macro when slicer change

▼魔方 西西 提交于 2021-02-10 17:45:55

问题


I can't get the macro to automatically run when an item in a slicer (from a chart) is selected. I tried by adding the code under a private sub or even public function but still can't get the click/selection of an item in the slicer to trigger the macro. I tested the sub Slicer_Opt and it works when I have an item selected and I run it manually, but as I said, I can't get it to automatically trigger when I select an item in the slicer. Until now I have the following:

Public Function ClickedSlicerItem(ByRef SlicerOpt1)
Call Slicer_Opt
End Function

Sub Slicer_Opt()

Dim LastMonth As String
Dim MiddleMonth As String
Dim FirstMonth As String
Dim Date1 As Date
Dim MonthMax As String
Dim MinMonth As Date
Dim SlicerMthCache As SlicerCache
Dim SlicerOpt1 As SlicerItem

Date1 = WorksheetFunction.Max(Sheets("Activity Log").Range("H:H"))
MinMonth = WorksheetFunction.Min(Sheets("Activity Log").Range("H:H"))
MonthMax = Month(Date1)
LastMonth = MonthName(MonthMax, True)
MiddleMonth = MonthName(MonthMax - 1, True)
FirstMonth = MonthName(MonthMax - 2, True)

If SlicerOpt1.ActiveItem Is Not Null Then 
    Application.ScreenUpdating = False
If ActiveWorkbook.SlicerCaches("Slicer_Last_Log_in").SlicerItems(LastMonth).Selected = True Then
        ActiveSheet.ChartObjects("Chart 2").Activate
        ActiveChart.ChartType = xlBarClustered
    ElseIf ActiveWorkbook.SlicerCaches("Slicer_Last_Log_in").SlicerItems(MiddleMonth).Selected = True Then
        ActiveSheet.ChartObjects("Chart 2").Activate
        ActiveChart.ChartType = xlBarClustered
    ElseIf ActiveWorkbook.SlicerCaches("Slicer_Last_Log_in").SlicerItems(FirstMonth).Selected = True Then
        ActiveSheet.ChartObjects("Chart 2").Activate
        ActiveChart.ChartType = xlBarClustered
    End If
    Application.ScreenUpdating = True
Else: ActiveChart.ChartType = xlBarStacked100
End If


End Sub

来源:https://stackoverflow.com/questions/53418302/run-macro-when-slicer-change

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!