VBA solution for moving pivot table filter range for week numbers

Dobo_Bobo

New Member
Joined
Jan 24, 2018
Messages
31
If it is possible please can someone provide the code that will enable me to automate the process of changing filters for week numbers on numerous pivot tables?


I produce a weekly report that requires a 10 week period range. Therefore, every week I have to remove from numerous pivot tables the Week filter that has become the 11th week and add to the Week filter the previous week.


I have provided a recorded macro version of the process and a screen shot of the table to help explain. I've tried various searches but I cannot find anything relevant. Any help that can be provided I will greatly appreciate as I am currently learning VBA.

Image: https://imgur.com/a/gYx4j



Code:
<code>Sub Macro2()
'
' Macro2 Macro
'
'
Range("B15").Select
ActiveSheet.PivotTables("TopPvt").PivotCache.Refresh
With ActiveSheet.PivotTables("TopPvt").PivotFields("Week")
    .PivotItems("45").Visible = False
    .PivotItems("3").Visible = True
End With
Range("B21").Select
End Sub</code>
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi Dobo;

Without having your workbook it's little bit hard for me to give you the exact answer, but I have something similar I'm using for similar situation.

Code below, when the date in cell D1 is chnaged, sets the filter of the pivot table to the date given in cell D1 and then refreshes the pivot table;

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo Err
If Intersect(Target, Range("D2")) Is Nothing Then Exit Sub


Dim pt1 As PivotTable
Dim Field1 As PivotField
Dim NewCat1 As Date


Set pt1 = Worksheets("Daily Report").PivotTables("PivotTable4")
Set Field1 = pt1.PivotFields("DATE")
NewCat1 = Worksheets("Daily Report").Range("D1").Value


With pt1
Field1.ClearAllFilters
Field1.CurrentPage = NewCat1
pt1.RefreshTable
End With


Err:
Exit Sub


End Sub

Hope this helps.
 
Upvote 0
Hi Taccoo

Thank you for taking the time to help me.

I've tried your code but I cannot get it to work due to Error 424. I've taken a screen shot so you can see what I mean:

https://imgur.com/buhNxGx

Can you see anywhere I have amended the code incorrectly? (I took out the 'Err' in order to see what the error is for me.)

I am not sure if it makes a difference but the week numbers are created using WEEKNUM function, thus are they seen as dates?
 
Upvote 0
Hi Dobo;

Please try;
Code:
Sub test()

Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim begin As Long
Dim finish As Long


On Error GoTo Err
begin = Range("c1")
finish = Range("d1")


Set pt = Sheets("Pivot").PivotTables("PivotTable1")
Set pf = pt.PivotFields("Week")


pf.ClearAllFilters
pf.EnableMultiplePageItems = True


For Each pi In pf.PivotItems
    pi.Visible = pi >= begin And pi <= finish
Next pi

pt.RefreshTable

Err:
Resume Next


End Sub

Cell C1 will be the beginning week number of your report and cell D1 will be the ending week of your report.

Regards
 
Last edited:
Upvote 0
Hi Taccoo

It works!! :) Thank you for your help! Your code will free up a lot of time which I can now use to learn VBA! I know a little VBA but having a macro that works will help me learn even more!

Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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