+ Reply to Thread
Results 1 to 3 of 3

Writing Slicer Items to Sheet- Modifying Existing Code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-23-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2013
    Posts
    262

    Writing Slicer Items to Sheet- Modifying Existing Code

    Hey All!

    Since Bakerman2 was kind enough to solve one of my long-running problems with the dashboard I'm working on, I have another to ask the community.

    With the following code, I am able to capture the selected slicer filters and have them update in a cell on my sheet in real time.

    My problem is that I have four slicers I want to do this with (each slicer's filters writing to a different cell), and I have not determined the way to do so yet.

    My slicer names are: Slicer_Month, Slicer_TWP_Code, Slicer_Activity_Code, Slicer_Program_Code.

    The cell formula for the first slicer would be:
    Formula: copy to clipboard
    =GetSelectedSlicerItems("Slicer_Month")


    The Public function that makes it possible is:

    Public Function GetSelectedSlicerItems(SlicerName As String) As String
        Dim oSc As SlicerCache
        Dim oSi As SlicerItem
        Dim lCt As Long
        On Error Resume Next
        Application.Volatile
        Set oSc = ThisWorkbook.SlicerCaches("Slicer_Month")
        If Not oSc Is Nothing Then
            For Each oSi In oSc.SlicerItems
                If oSi.Selected Then
                    GetSelectedSlicerItems = GetSelectedSlicerItems & oSi.Name & ", "
                    lCt = lCt + 1
                ElseIf oSi.HasData = False Then
                    lCt = lCt + 1
                End If
            Next
            If Len(GetSelectedSlicerItems) > 0 Then
                If lCt = oSc.SlicerItems.Count Then
                    GetSelectedSlicerItems = "All Available Data Selected"
                Else
                    GetSelectedSlicerItems = Left(GetSelectedSlicerItems, Len(GetSelectedSlicerItems) - 2)
                End If
            Else
                GetSelectedSlicerItems = "No items selected"
            End If
        Else
            GetSelectedSlicerItems = "No slicer with name '" & SlicerName & "' was found"
        End If
    End Function
    I tried repeating this function four times with the slicer names but that resulted in Ambiguous name errors. I also tried inputting multiple slicer names into the slicercaches area of the sheet but that also did not work.

    Any thoughts from my Excel Forum Heroes?

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

    Re: Writing Slicer Items to Sheet- Modifying Existing Code

    This line:
    Set oSc = ThisWorkbook.SlicerCaches("Slicer_Month")
    is hard coded. Since you pass the slicer name to the function change it to read
    Set oSc = ThisWorkbook.SlicerCaches(SlicerName)

  3. #3
    Forum Contributor
    Join Date
    05-23-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2013
    Posts
    262

    Re: Writing Slicer Items to Sheet- Modifying Existing Code

    Hi dflak!

    I ended up contacting the original code writer and he returned my question with an answer.

    You are absolutely correct, however, (and I didn't realize it till his second solution) that "SlicerName" didn't mean "input your slicer name" ha.

    I've been meaning to get back here to post the solution so I'm glad your reply sent me a reminder.

    Here was his response:

    Here is an alternative version which has an extra argument with which you can choose to either get all items in a single cell, or return a list of them. For the latter to work you must array-enter the formula into as many cells as you expect to have sliceritems.

    Public Function GetSelectedSlicerItems(SlicerName As String, Optional ReturnArray As Boolean = False) As Variant 
        Dim oSc As SlicerCache 
        Dim oSi As SlicerItem 
        Dim lCt As Long 
        On Error Resume Next 
        Application.Volatile 
        Set oSc = ActiveWorkbook.SlicerCaches(SlicerName) 
        If Not oSc Is Nothing Then 
            For Each oSi In oSc.SlicerItems 
                If oSi.Selected Then 
                    GetSelectedSlicerItems = GetSelectedSlicerItems & oSi.Name & ", " 
                    lCt = lCt + 1 
                ElseIf oSi.HasData = False Then 
                    lCt = lCt + 1 
                End If 
            Next 
            If Len(GetSelectedSlicerItems) > 0 Then 
                If lCt = oSc.SlicerItems.Count Then 
                    GetSelectedSlicerItems = "All Items" 
                Else 
                    GetSelectedSlicerItems = Left(GetSelectedSlicerItems, Len(GetSelectedSlicerItems) - 2) 
                    If ReturnArray Then 
                        GetSelectedSlicerItems = Application.Transpose(Split(GetSelectedSlicerItems, ",")) 
                    End If 
                End If 
            Else 
                GetSelectedSlicerItems = "No items selected" 
            End If 
        Else 
            GetSelectedSlicerItems = "No slicer with name '" & SlicerName & "' was found" 
        End If 
    End Function
    Since I have four slicers, I entered this formula (below) into four cells (each with the correct slicer name) and then Array-Entered each one separately.

    And Voila.

    Formula: copy to clipboard
    =GetSelectedSlicerItems("Slicer_Month")

+ 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. Modifying existing code to save worksheet
    By Woopwoop in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-26-2015, 08:54 PM
  2. Modifying existing code
    By rhouston08 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-13-2015, 06:03 PM
  3. [SOLVED] Add two more columns, modifying an existing VBA code help.
    By Anka in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 02-17-2013, 01:24 PM
  4. Modifying Existing Code to Check if the entry exists
    By jason_kelly in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-31-2010, 10:39 AM
  5. Modifying existing VBA code to find different file name
    By PittsburghEng in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-03-2010, 03:24 PM
  6. Help modifying existing VBA code
    By camcafe in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-28-2008, 11:02 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