Slicer update based on cell value

crystalneedshelpplzthnx

Board Regular
Joined
Nov 24, 2017
Messages
55
Office Version
  1. 365
Platform
  1. Windows
Hi,

I run a report 7 days a week and have a few slicers per page. I have the slicers linked, but I need to update two of the slicers everyday to the latest date and the latest week. I have tried several slicers after looking online:

Code:
Sub Filter_PivotField()


Dim sSheetName As String
Dim sPivotName As String
Dim sFieldName As String
Dim sFilterCrit As String
Dim pi As PivotItem


    sSheetName = "Sales Summary"
    sPivotName = "Slicer_Day_of_Date"


'I'm not sure if this name is correct, I'm not sure what the code needs:
    sFieldName = "Day of Date"


'I get subscript our of range error here:
sFilterCrit = ThisWorkbook.Worksheets("Chart Data").Range("p10").Value


With ThisWorkbook.Worksheets(sSheetName).PivotTables(sPivotName).PivotFields(sFieldName)


For Each pi In .PivotItems
If pi.Name <> sFilterCrit Then
pi.Visible = False

End If

Next pi


End With


End Sub

When I manually select slicers I get this:
Code:
Sub Macro1()
    With ActiveWorkbook.SlicerCaches("Slicer_Day_of_Week")
        .SlicerItems("3/3/2018").Selected = True
        .SlicerItems("2/24/2018").Selected = False
        .SlicerItems("2/17/2018").Selected = False
    End With
    With ActiveWorkbook.SlicerCaches("Slicer_Day_of_Date")
        .SlicerItems("3/1/2018").Selected = True
        .SlicerItems("2/28/2018").Selected = False
        .SlicerItems("2/27/2018").Selected = False
        .SlicerItems("2/26/2018").Selected = False
        .SlicerItems("2/25/2018").Selected = False
        .SlicerItems("2/24/2018").Selected = False
        .SlicerItems("2/23/2018").Selected = False
        .SlicerItems("2/22/2018").Selected = False
        .SlicerItems("2/21/2018").Selected = False
        .SlicerItems("2/20/2018").Selected = False
        .SlicerItems("2/19/2018").Selected = False
        .SlicerItems("2/18/2018").Selected = False
        .SlicerItems("2/17/2018").Selected = False
        .SlicerItems("2/16/2018").Selected = False
        .SlicerItems("2/15/2018").Selected = False
        .SlicerItems("2/14/2018").Selected = False
        .SlicerItems("2/13/2018").Selected = False
        .SlicerItems("2/12/2018").Selected = False
        .SlicerItems("2/11/2018").Selected = False
    End With
End Sub

I would prefer to loop through all available selections until the date in the cell is found. The cell is on another tab, but can be hidden on same tab is necessary.

Also, where would I store the code. Is it possible to save in my Personal workbooks module?

Thank you
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I just found this and it works great to select all options in all the slicers. I'm sure it can be used to select 1 option on 1 slicer based on a cell value. What do you think?

Code:
Sub SlicerReset()
    
Dim sc As SlicerCache
Dim sl As Slicer
Dim i As Integer
For Each sc In ActiveWorkbook.SlicerCaches
    For Each sl In sc.Slicers
        For i = 1 To sc.SlicerItems.Count
            sc.SlicerItems(i).Selected = True
        Next i
    Next sl
Next sc


End Sub
 
Upvote 0
I found one that WORKS!!! But I need to reference a cell value on another sheet.
Can anyone assist with incorporating


Tab "Chart Data" Cell "P10"

into

Code:
Sub itworks()


For Each slcCache In ActiveWorkbook.SlicerCaches
        slcCache.ClearManualFilter
Next


With ActiveWorkbook.SlicerCaches("Slicer_Day_of_Date")
    For Each oSlicerItem In .SlicerItems
        If oSlicerItem.Name = "2/12/2018" Then
            oSlicerItem.Selected = True
        Else
            oSlicerItem.Selected = False
        End If
    Next oSlicerItem
End With
End Sub
 
Upvote 0
Found answer on another site for anyone who is interested:

To select the last Slicer item, try something like this

Code:
[COLOR=#333333][I]Sub test()
    Dim i As Long, n As Long
    
    With ActiveWorkbook.SlicerCaches("Slicer_Account")
        n = .SlicerItems.Count
        
        For i = 1 To n - 1
            .SlicerItems(i).Selected = False
        Next I
        
        .SlicerItems(n).Selected = True
    End With
End Sub[/I][/COLOR]

Modded for my purposes...

Code:
Sub Update_Slicer_Date()
    Dim i As Long, n As Long
    
    With ActiveWorkbook.SlicerCaches("Slicer_Day_of_Week")
        n = .SlicerItems.Count
        
        For i = 2 To n - 0
            .SlicerItems(i).Selected = False
        Next i
        
        .SlicerItems(n).Selected = False
    End With
    
        With ActiveWorkbook.SlicerCaches("Slicer_Day_of_Date")
        n = .SlicerItems.Count
        
        For i = 2 To n - 0
            .SlicerItems(i).Selected = False
        Next i
        
        .SlicerItems(n).Selected = False
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,436
Messages
6,124,869
Members
449,192
Latest member
MoonDancer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top