+ Reply to Thread
Results 1 to 7 of 7

Random slicer value selected automatically

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-01-2017
    Location
    Santos
    MS-Off Ver
    Office 2016 - Windows 7
    Posts
    105

    Random slicer value selected automatically

    Dears good afternoon,

    I was wondering if someone know a way through VBA to select a value on a slicer randomly and automatically every 2 minutes.
    I want to create a dahsboard, visible on TV and to avoid someone to change the value by himself, I am looking for a solution through VBA.

    If someone has this kind of solution I will be grateful.

    P.S : I still donīt have an excel file. I am just wondering if it is possible and what should be the code in case itīs possible ?

    Waiting for your answer
    Regards

  2. #2
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371

    Re: Random slicer value selected automatically

    Yes, it is possible. I would recommend cycling through slicer items rather than randomising because it's easier, otherwise you would have to code in 'human random' as opposed to 'actual random' (remember when Apple had to make the iPod shuffle less random because users didn't like it).

    Here's something for you to work with on the selecting slicer items part:
    Private Sub SelectNextSlicerItem()
    
        On Error Resume Next
        
        Const sSlicerName   As String = "Slicer_mySlicer"
        
        Static iPos         As Integer
        Static sc           As SlicerCache
        
        Dim i               As Integer
        Dim bAction         As Boolean
        
        'Set slicer and check
        If sc Is Nothing Then Set sc = ThisWorkbook.SlicerCaches(sSlicerName)
        If sc Is Nothing Then GoTo Exit_Proc
        
        'Turn off updating/events
        bAction = True
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        'Increase counter, loop if end reached
        If iPos >= sc.SlicerItems.Count Then iPos = 0
        iPos = iPos + 1
        'Reset filter
        sc.ClearAllFilters
        'Perform selection action
        For i = 1 To sc.SlicerItems.Count
            sc.SlicerItems(i).Selected = (i = iPos)
        Next i
        
    Exit_Proc:
        If bAction Then
            With Application
                .ScreenUpdating = True
                .EnableEvents = True
            End With
        End If
        Exit Sub
        
    End Sub
    Design everything to be as simple as possible, but no simpler.

  3. #3
    Forum Contributor
    Join Date
    06-01-2017
    Location
    Santos
    MS-Off Ver
    Office 2016 - Windows 7
    Posts
    105

    Re: Random slicer value selected automatically

    Stormin good morning

    First of all thank you for your help.
    This code is working but...

    First of all, I am trying to get this code working alone on a loop.
    My pivot table is refreshing every 5 mimnutes. What I need is to integrate this code in my routine.

    Then, I saw that once the code is selecting all the items in the slicers, is not resetting the slicer.
    Instead, itīs going back to the first item in the slicer.
    What I was wondering is if you code can reset the slicer and then starting again, selecting the first item ?

    See below my code where i need to integrate your code :

    Sub PivotMacro()
    Dim pt As PivotTable
        Dim rng As Range
        Dim i As Long
    Application.ScreenUpdating = False
    
    Workbooks("cs_tools_v3.xlsm").Sheets("Dashboard").Activate
      
    Sheets("Database").Select
    
    
        With ActiveSheet.ListObjects("Table_brsszccwvs0002_sql_live_BPM_LIVE_vw_BJF")
            .Range.AutoFilter
            .Range.AutoFilter Field:=.ListColumns("K/P").Index, _
                Criteria1:="=delete"
            .Range.Offset(1).EntireRow.Delete
            .Range.AutoFilter
        End With
        
    Sheets("Results").Select
    
        Set pt = ActiveSheet.PivotTables("PivotTable1")
    
        pt.RefreshTable
        
    Application.ScreenUpdating = True
    
    Sheets("Dashboard").Select
    
    End Sub

    Actually my code is still not running alone.
    I have created a button to run this code.
    But has I said above, I need to run this code (alone) every 5 minutes.

    If you can help me, I will appreciate.
    Regards

  4. #4
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371

    Re: Random slicer value selected automatically

    Hiya,

    My code selects the next item each time you run it. Just integrate my code into your loop, call it to move to the next item. It's just a base starting point so you can customise it further.

    There are many many other posts on refreshing data connections for TV dashboards, there's lots of examples of that code out there. However that's not the subject of this thread and so--after searching and trying existing solutions--if you cannot get it to work I would recommend starting a new thread on that subject.

    Hope that helps

  5. #5
    Forum Contributor
    Join Date
    06-01-2017
    Location
    Santos
    MS-Off Ver
    Office 2016 - Windows 7
    Posts
    105

    Re: Random slicer value selected automatically

    Storminī
    thank you for your answer
    I found what I was looking for.

    Just one last question.
    In your code, I am wondering how to let the code understand that after every loop selecting item by item, we need to clear the slicer to get all items selected and then once again one by one ?

    Can you please help me ?
    Regards

  6. #6
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371

    Re: Random slicer value selected automatically

    Quick mod could be:

    Private Sub SelectNextSlicerItem()
    
        On Error Resume Next
        
        Const sSlicerName   As String = "Slicer_mySlicer"
        
        Static iPos         As Integer
        Static sc           As SlicerCache
        
        Dim i               As Integer
        Dim bAction         As Boolean
        
        'Set slicer and check
        If sc Is Nothing Then Set sc = ThisWorkbook.SlicerCaches(sSlicerName)
        If sc Is Nothing Then GoTo Exit_Proc
        
        'Turn off updating/events
        bAction = True
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        'Increase counter, loop if end reached
        If iPos > sc.SlicerItems.Count Then iPos = 0
        iPos = iPos + 1
        'Reset filter
        sc.ClearAllFilters
        'Perform selection action
        If iPos <= sc.SlicerItems.Count Then
            For i = 1 To sc.SlicerItems.Count
                sc.SlicerItems(i).Selected = (i = iPos)
            Next i
        End If
        
    Exit_Proc:
        If bAction Then
            With Application
                .ScreenUpdating = True
                .EnableEvents = True
            End With
        End If
        Exit Sub
        
    End Sub

  7. #7
    Forum Contributor
    Join Date
    06-01-2017
    Location
    Santos
    MS-Off Ver
    Office 2016 - Windows 7
    Posts
    105

    Re: Random slicer value selected automatically

    Iīll check it now and let you know asap
    thanks for your help

    EDIT : YES !!!!!! it works !!! thanks a lot
    Last edited by ozstrik3r69; 01-16-2018 at 12:57 PM.

+ 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. Finding Our Selected Slicer Value
    By NeedForExcel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-28-2017, 07:52 AM
  2. [SOLVED] Changing the Chart Title automatically when a value in Slicer is selected
    By rv02 in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 12-03-2014, 06:05 PM
  3. Finding Items NOT selected on a Slicer
    By david2003 in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 11-05-2014, 08:13 AM
  4. Use selected data from slicer
    By Tjken in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 10-22-2014, 07:18 AM
  5. Copy selected value from slicer
    By merdle in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-29-2014, 04:56 PM
  6. VBA for displaying selected slicer in cell S7
    By crobledo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-11-2013, 03:14 PM
  7. Replies: 2
    Last Post: 04-18-2013, 09:10 AM

Tags for this Thread

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