+ Reply to Thread
Results 1 to 7 of 7

Disconnect slicers, dynamically update pivot table data source range et reconnect

Hybrid View

Vincent121 Disconnect slicers,... 06-08-2016, 02:23 AM
romperstomper Re: Disconnect slicers,... 06-08-2016, 03:16 AM
Vincent121 Re: Disconnect slicers,... 06-08-2016, 08:46 AM
Vincent121 Re: Disconnect slicers,... 06-08-2016, 08:49 AM
romperstomper Re: Disconnect slicers,... 06-08-2016, 11:04 AM
Vincent121 Re: Disconnect slicers,... 06-09-2016, 03:03 AM
romperstomper Re: Disconnect slicers,... 06-10-2016, 05:30 AM
  1. #1
    Registered User
    Join Date
    05-11-2016
    Location
    OTA
    MS-Off Ver
    2010
    Posts
    10

    Disconnect slicers, dynamically update pivot table data source range et reconnect

    Hey everyone, I'm a noob trying to learn. I built a dashboard with charts and slicers made from pivot tables. I would love to be able to update the data source range automatically (which requires disconnecting all slicers, updating and then reconnecting everything) using VBA.

    I found this piece of code but I don't really know how to use it...

    Can you help me please?

    Thank you very much,

    vincent

    Dim PT                          As PivotTable
       Dim ptMain                      As PivotTable
       Dim ws                          As Worksheet
       Dim oDic                        As Object
       Dim oPivots                     As Object
       Dim i                           As Long
       Dim lIndex                      As Long
       Dim Max                         As Long
       Dim vPivots
       Dim vSlicers
       Dim vItem
    
       vSlicers = Array("REG", "DIRECTOR", "AREA_MANAGER", "AOI", "DMA", "AG", "CLOCK")
       Set oDic = CreateObject("Scripting.Dictionary")
    
       Max = Sheets("eLink_Raw").Cells(Rows.Count, "A").End(xlUp).Row
    
       ' disconnect slicers
       For Each vItem In vSlicers
          With ActiveWorkbook.SlicerCaches("Slicer_" & vItem).PivotTables
             If .Count > 0 Then
                Set oPivots = CreateObject("Scripting.Dictionary")
                For i = .Count To 1 Step -1
                   oPivots.Add .Item(i).Name, .Item(i)
                   .RemovePivotTable .Item(i)
                Next i
                oDic.Add vItem, oPivots
             End If
          End With
       Next vItem
    
       ' update pivottables
       For Each ws In ActiveWorkbook.Worksheets
          For Each PT In ws.PivotTables
             If lIndex = 0 Then
                PT.ChangePivotCache _
                      ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
                                                        SourceData:=Sheets("eLink_Raw").Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1))
                Set ptMain = PT
                lIndex = 1
             Else
                PT.CacheIndex = ptMain.CacheIndex
             End If
          Next PT
       Next ws
    
       ' reconnect slicers
       For Each vItem In vSlicers
          If oDic.Exists(vItem) Then
             Set oPivots = oDic(vItem)
             vPivots = oPivots.Items
             For i = LBound(vPivots) To UBound(vPivots)
                ActiveWorkbook.SlicerCaches("Slicer_" & vItem).PivotTables.AddPivotTable vPivots(i)
             Next i
          End If
       Next vItem
    
       Set oDic = Nothing

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,138

    Re: Disconnect slicers, dynamically update pivot table data source range et reconnect

    It looks like the only thing you need to change is this line:
    vSlicers = Array("REG", "DIRECTOR", "AREA_MANAGER", "AOI", "DMA", "AG", "CLOCK")
    to match the names of the slicers you need to disconnect.

  3. #3
    Registered User
    Join Date
    05-11-2016
    Location
    OTA
    MS-Off Ver
    2010
    Posts
    10

    Re: Disconnect slicers, dynamically update pivot table data source range et reconnect

    Thanks

    There seems to be other parameters I need to change like "eLink_Raw" in
    Max = Sheets("eLink_Raw").Cells(Rows.Count, "A").End(xlUp).Row
    Also I can't figure out what "Slicer_" mean in this line of code:
     With ActiveWorkbook.SlicerCaches("Slicer_" & vItem).PivotTables
    Thanks for your help!

  4. #4
    Registered User
    Join Date
    05-11-2016
    Location
    OTA
    MS-Off Ver
    2010
    Posts
    10

    Re: Disconnect slicers, dynamically update pivot table data source range et reconnect

    the error comes at
    With ActiveWorkbook.SlicerCaches("Slicer_" & vItem).PivotTables
    Last edited by Vincent121; 06-08-2016 at 09:11 AM.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,138

    Re: Disconnect slicers, dynamically update pivot table data source range et reconnect

    By default the slicer cache is called Slicer_ followed by the name of the field you attached the slicer to. What did you alter the array to?

  6. #6
    Registered User
    Join Date
    05-11-2016
    Location
    OTA
    MS-Off Ver
    2010
    Posts
    10

    Re: Disconnect slicers, dynamically update pivot table data source range et reconnect

    thanks, I added my slicers.. for some reason the "Slicer_" seems to cause errors in my code

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,138

    Re: Disconnect slicers, dynamically update pivot table data source range et reconnect

    What does:
    Msgbox Activeworkbook.Slicercaches(1).name
    show?

+ 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. Can I update data source from pivot table?
    By elbrujo in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-02-2016, 10:25 PM
  2. [SOLVED] Disconnect Slicers, Change Pivot Table Source, Reconnect Slicers: a problem
    By Geoff. in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-30-2015, 02:27 PM
  3. using VBA to update a pivot table data source
    By bzl in forum Excel General
    Replies: 1
    Last Post: 06-03-2015, 04:38 PM
  4. change(update) pivot table data source.
    By Berserk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-09-2012, 08:21 AM
  5. Update source data from pivot table
    By bryanbak3 in forum Excel General
    Replies: 0
    Last Post: 01-12-2012, 05:28 PM
  6. Update Pivot table using a dynamic range for source data
    By Carl Thompson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-02-2011, 08:20 AM
  7. [SOLVED] DYNAMICALLY data source for pivot table
    By Kalyan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-12-2006, 11:10 PM

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