This is "Air Code." I have nothing to test it on.
Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, _
ByVal Target As PivotTable)
'--when pivot update event triggered, checks whether a specified slicer
' has more than one item selected.
' If so, user is warned and optionally the last action can be undone.
CheckSlicer "Slicer_Exp", "Selection Limited to either OPEX or CAPEX, but not both."
CheckSlicer "Next_Slicer", "Next message"
End Sub
Sub CheckSlicer(sSlicerName As String, SlicerMsg As String)
sLastUndoStackItem = Application.CommandBars("Standard").FindControl(ID:=128).List(1)
'--validate event was triggered by slicer or filter, not other pivot operation
Select Case sLastUndoStackItem
Case "Slicer Operation", "Filter"
'continue
Case Else
'do nothing and exit
GoTo ExitProc
End Select
'--validate specified slicer exists
On Error Resume Next
Set slc = SlicerCaches(sSLICER_NAME)
On Error GoTo 0
If slc Is Nothing Then
GoTo ExitProc
End If
'--validate pvt that triggered event is connected to specified slicer
For Each pvt In slc.PivotTables
If pvt.Name = Target.Name Then
bSlicerIsConnected = True
Exit For
End If
Next pvt
'--test how many items selected and take action if more than one
If bSlicerIsConnected Then
If slc.VisibleSlicerItems.Count > 1 Then
'--option a: only warn user
'MsgBox "Selection Limited to either OPEX or CAPEX, but not both." & vbCr _
' & "Please undo last selection."
'--option b: warn user and undo
MsgBox SlicerMsg
With Application
.EnableEvents = False
.Undo
End With
End If
End If
ExitProc:
Application.EnableEvents = True
End Sub
Bookmarks