Pivot Table - Dynamic Data Source range change via VBA

FaraDmX

New Member
Joined
Jan 30, 2020
Messages
4
Office Version
  1. 2013
  2. 2010
Platform
  1. 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
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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.
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,837
Messages
6,121,883
Members
449,057
Latest member
Moo4247

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