This is a follow-up to a thread from several years ago. The forum recommended I start a new thread. I'm using the following code (based on code from this forum) to enforce a single-select slicer. I cannot figure out why it's triggering run-time error 1004: "Application-defined or object-defined error" on the "slc.VisibleSlicerItems.Count" line. Thanks!
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim bSlicerIsConnected As Boolean
Dim pvt As PivotTable
Dim slc As SlicerCache
Dim sLastUndoStackItem As String
With Application
.Calculation = xlCalculationManual
.DisplayAlerts = False
.ScreenUpdating = False
.EnableCancelKey = False
End With
' Identify target slicer
Const MySlicer As String = "Slicer_TEAM3"
Const strSubject As String = "team"
' Get undo stack and handle empty stack scenario
On Error Resume Next
sLastUndoStackItem = Application.CommandBars("Standard").FindControl(ID:=128).List(1)
On Error GoTo 0
' Verify event triggered by slicer or filter
Select Case sLastUndoStackItem
Case "Slicer Operation", "Filter"
Case vbNullString: GoTo GOODBYE ' Undo stack empty
Case Else: GoTo GOODBYE
End Select
' Confirm slicer exists
On Error Resume Next
Set slc = ActiveWorkbook.SlicerCaches(MySlicer)
On Error GoTo 0
If slc Is Nothing Then GoTo GOODBYE
' Verify pivot table that triggered event is connected to slicer
For Each pvt In slc.PivotTables
If pvt.Name = Target.Name Then bSlicerIsConnected = True: Exit For
Next pvt
If Not bSlicerIsConnected Then GoTo GOODBYE
' Determine number of selected items
If slc.VisibleSlicerItems.Count = 1 Then GoTo GOODBYE
' Display error and undo
MsgBox "You can only select one " & strSubject & "."
With Application
.EnableEvents = False
.Undo
End With
GOODBYE:
With Application
.Calculation = xlAutomatic
.DisplayAlerts = True
.ScreenUpdating = True
.EnableCancelKey = True
End With
End Sub
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim bSlicerIsConnected As Boolean
Dim pvt As PivotTable
Dim slc As SlicerCache
Dim sLastUndoStackItem As String
With Application
.Calculation = xlCalculationManual
.DisplayAlerts = False
.ScreenUpdating = False
.EnableCancelKey = False
End With
' Identify target slicer
Const MySlicer As String = "Slicer_TEAM3"
Const strSubject As String = "team"
' Get undo stack and handle empty stack scenario
On Error Resume Next
sLastUndoStackItem = Application.CommandBars("Standard").FindControl(ID:=128).List(1)
On Error GoTo 0
' Verify event triggered by slicer or filter
Select Case sLastUndoStackItem
Case "Slicer Operation", "Filter"
Case vbNullString: GoTo GOODBYE ' Undo stack empty
Case Else: GoTo GOODBYE
End Select
' Confirm slicer exists
On Error Resume Next
Set slc = ActiveWorkbook.SlicerCaches(MySlicer)
On Error GoTo 0
If slc Is Nothing Then GoTo GOODBYE
' Verify pivot table that triggered event is connected to slicer
For Each pvt In slc.PivotTables
If pvt.Name = Target.Name Then bSlicerIsConnected = True: Exit For
Next pvt
If Not bSlicerIsConnected Then GoTo GOODBYE
' Determine number of selected items
If slc.VisibleSlicerItems.Count = 1 Then GoTo GOODBYE
' Display error and undo
MsgBox "You can only select one " & strSubject & "."
With Application
.EnableEvents = False
.Undo
End With
GOODBYE:
With Application
.Calculation = xlAutomatic
.DisplayAlerts = True
.ScreenUpdating = True
.EnableCancelKey = True
End With
End Sub