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

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,402
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,402
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,165
Messages
5,485,134
Members
407,481
Latest member
junniec

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top