+ Reply to Thread
Results 1 to 4 of 4

Limiting slicer selection to 1 item for two different slicers

Hybrid View

  1. #1
    Registered User
    Join Date
    08-01-2018
    Location
    Abu Dhabi, UAE
    MS-Off Ver
    Excel 2016
    Posts
    5

    Limiting slicer selection to 1 item for two different slicers

    Hi Everyone,

    - I am trying to get to iSlicers to limit their selection to only one item at a time. i have gotten it done for one of the islicers but cant seem to introduce the same for the other one.
    - The code i am using is as follows:


    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.
    
     Dim bSlicerIsConnected As Boolean
     Dim pvt As PivotTable
     Dim slc As SlicerCache
     Dim sLastUndoStackItem  As String
    
     '--modify this to match your slicer's name
     Const sSLICER_NAME As String = "Slicer_Exp"
    
     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 "Selection Limited to either OPEX or CAPEX, but not both."
          With Application
             .EnableEvents = False
             .Undo
          End With
       End If
     End If
    ExitProc:
       Application.EnableEvents = True
    End Sub


    - As you can see in the above code, the name of my slicer is "Slicer_Exp", and it works perfectly fine.
    - Is there anyway i could introduce another slicer i have called "Slicer_M." to this code or would i have to create a code in another module?

    Thanks,

    Abdulla
    Last edited by aalbarak; 08-02-2018 at 02:55 AM.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Limiting slicer selection to 1 item for two different slicers

    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
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Limiting slicer selection to 1 item for two different slicers

    Of course, there is another solution. You could use a combo box or data validation that allows only one selection, put a change sheet event on the cells and then use the value to update the pivot table filters.

  4. #4
    Registered User
    Join Date
    08-01-2018
    Location
    Abu Dhabi, UAE
    MS-Off Ver
    Excel 2016
    Posts
    5

    Re: Limiting slicer selection to 1 item for two different slicers

    I have attached a sample file.
    If you go to VBA, you'll see that the slicer named Slicer_Exp is the one which already uses the code to limit it to a single selection. I would also like to have the slicer named Slicer_M. to be part of that same code and to do the same thing.

    Thanks,

    Abdulla
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Power Query code to test for multiple conditions
    By kersplash in forum Excel Formulas & Functions
    Replies: 30
    Last Post: 07-18-2018, 04:51 AM
  2. [SOLVED] Modify existing code to act on multiple conditions
    By MikeWaring in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-26-2018, 03:11 PM
  3. [SOLVED] How to Simplify Multiple IF Conditions in VBA Code.
    By zaska in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-10-2016, 03:13 AM
  4. VBA code required to run after checkbox value is true and test multiple conditions.
    By mchilapur in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-10-2015, 08:30 PM
  5. [SOLVED] VBA code to delete duplicate rows based on multiple conditions
    By pjsween in forum Excel General
    Replies: 5
    Last Post: 06-27-2012, 01:15 PM
  6. Cut & Paste Multiple Sheet Conditions Macro Code-Check
    By BrokenHero in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-06-2009, 01:26 AM
  7. Code for Multiple If conditions
    By Raushan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-25-2007, 08:23 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1