Pivot Table - Dynamic Data Source range change via VBA

FaraDmX

New Member
Joined
Jan 30, 2020
Messages
4
Office Version
2013, 2010
Platform
Windows
Hi all,

Currently, I am working on the project with 7 different pivot tables stored on 7 seperate work sheets and the main sheet where i keep data extract. So I decided to use VBA that every time when I update my data extract to automate this process with the help of macros and VBA. I could compile a code that automatiocay captures newly added data and updates the pivot tables. However, every time I when i run the code for all pivot tables simultaneously, pivot tables disappear in the Report Connectios of Slicers. I checked that when I manually update data for all pivot tables I still can connect slicer to all of them, however, with current code, I can not see them in Report Connections window.

Could you please advise on that threat.
Thanks in advance!
BR, Fara
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,390
Create a defined name that dynamically covers data (adjusting automatically as data is added), like:
Defined name of MyData:
Definition:
=OFFSET($A$1,0,0,COUNTA(A:A),14)
(14 columns wide)
Then use that as the source for each PivotTable ... on refresh they will include all data. I use the Refresh All ribbon button to update all PivotTables at the same time.
 

FaraDmX

New Member
Joined
Jan 30, 2020
Messages
4
Office Version
2013, 2010
Platform
Windows
First of all thank you. Can you please provide a VBA code, how would it look like via VBA? I have to do it with VBA and macros. Thanks
 

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,390
First of all thank you. Can you please provide a VBA code, how would it look like via VBA? I have to do it with VBA and macros. Thanks
Explain why you have to do it with VBA and macros?
 

FaraDmX

New Member
Joined
Jan 30, 2020
Messages
4
Office Version
2013, 2010
Platform
Windows
Here you go:

Substantially reduces the number of Pivot Caches. Do not ament anything just copy, paste and run.

VBA Code:
Sub changeCache()
Dim ws As Worksheet
Dim pt As PivotTable
Dim pc As PivotCache
Dim first As Boolean
On Error Resume Next

    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        For Each pt In ActiveSheet.PivotTables

            If first = False Then
                Set pc = pt.PivotCache
                first = True
            End If

            pt.CacheIndex = pc.Index

        Next pt
    Next ws

End Sub

You are welcome.
 

Forum statistics

Threads
1,089,434
Messages
5,408,195
Members
403,188
Latest member
Sanjana Ramesh

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top