Filtering Data by Date into New Sheet and Change Data Source of Multiple Pivot Table

ismaill

New Member
Joined
Apr 24, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Hello everyone,

I recently start using VBA and Mr.Excel is helping me a lot during this learning process. However there is an issue that I couldn't solve by my own.

I've a Master_DATA sheet, in which all data is comes through a query from OneDrive. And there are several pivot tables in which source is a Table "DATA" in Master_DATE Sheet. All pivots are connected to Pivot Charts. These charts are on Dashboard Sheet.

I need a macro which apply filter to "DATA" Table according to "Exp. Closing Date" column (or column N) to get before today's date, and then the macro should copy filtered data into new sheet which named "DATA_OD". Then, I'd like to make this new sheet as source of all Pivot Tables. So, charts on the dashboard automatically will be updated.

Pivot tables are on 3 different worksheet.

The way I've tried is here:

First, I used this formula on the new sheet ("DATA OD") to filter and copy data :
Code:
=FILTER(DATA;DATA[Exp. Closing Date]<TODAY())

Then use below macro:
VBA Code:
Sub AdjustPivotDataRange()

  Dim pt As PivotTable, pc As PivotCache
    Dim dataSheet As Worksheet, ws As Worksheet
    Dim startPoint As Range, dataSource As Range, newRange As String

    ' get worksheet with data
    Set dataSheet = ThisWorkbook.Worksheets("DATA_OD")

    ' Dynamically Retrieve Range Address of Data
    Set startPoint = dataSheet.Range("A1")
    Set dataSource = dataSheet.Range(startPoint, startPoint.SpecialCells(xlLastCell))
    newRange = dataSheet.Name & "!" & dataSource.Address(ReferenceStyle:=xlR1C1)

    ' create new PivotCache
    Set pc = ActiveWorkbook.PivotCaches.Create( _
               SourceType:=xlDatabase, _
               SourceData:=newRange)

    ' loop through all tables in all sheets
    For Each ws In ActiveWorkbook.Worksheets
        For Each pt In ws.PivotTables

            ' update pivot source and refresh
            pt.ChangePivotCache pc
            pt.RefreshTable

        Next pt
    Next ws

End Sub

I also need a second macro to turn everything back but I think I can handle that by myself.

Hope the explanation is clear for you. Thanks in advance.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,214,614
Messages
6,120,530
Members
448,969
Latest member
mirek8991

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