简体   繁体   中英

Change Pivot Table Filter Using Excel

I'm developing a solution for some colleagues to use and am trying to automate/simplify as much of it as possible.

My colleagues have a master table of information that is continually added to from external sources by copy/paste. An example of the table can be found here

Existing data in the above is example is in black and new data (red) is pasted below it. This table is found on a Worksheet named 'Data'

The information is then summarised by a Pivot Table named 'Summary' on a separate Worksheet called 'Pivot' and looks like this

I've set up a button for my colleague's to hit which refreshes the Pivot Table with the new data.

However, I was looking for a way to change the change the filter of the 'Summary' Pivot Table to only show the information for the most recently added information. In the example above, it would hide the orders for the 31/04/2018 and show the orders for 01/05/2018.

I've put together the below but am unsure if it's sensible. Or how I would render all other dates invisible without enumerating them all.

Any help would be greatly appreciated.

Thanks

Sub PivotUpdate()

Dim CurrDate As Date

Worksheets("Data").Activate
CurrDate = Cells(.Rows.Value, "A").End(xlUp).Row

Workbook.RefreshAll

Worksheets("Pivot").Activate
With ActiveSheet.PivotTables("Summary").PivotFields("Date")
    'Clear all other filter items
    PivotItems(CurrDate).Visible = True
End With

End Sub

You could use the pretty comprehensive function I posted at Filtering pivot table with vba to do this. Lots of images at that answer showing the kinds of things it handles, including showing the most recent x days worth of data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM