(VBA)OLAP pivot table- Filtering both year and month in Visibleitemslist

Madmax24

New Member
Joined
Feb 3, 2022
Messages
1
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I have been trying to automate this macro that's filtering the date range depending on the input date field(I have given the input date as July 2022). This date range filter should be applied in such a way that it should include three years before the input year(i.e. 2020-2022) and all the months before the input month(jan-july).
I'm not sure how to create a generalized for loop for the filter below that will automatically change the year and month according to the input field. I have tried to create a for loop for just the month for the current year but don't know how to combine the ranges for the previous three years as well.

Sub Macro2()
'
' Macro2 Macro
'

'
ActiveSheet.PivotTables("NetRate22").PivotFields( _
"[Policy State Dimensions].[Effective Date].[Effective Year]"). _
VisibleItemsList = Array("")
ActiveSheet.PivotTables("NetRate22").PivotFields( _
"[Policy State Dimensions].[Effective Date].[Effective Month]"). _
VisibleItemsList = Array( _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[1]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[2]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[3]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[4]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[5]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[6]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[7]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[1]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[2]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[3]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[4]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[5]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[6]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[7]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[1]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[2]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[3]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[4]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[5]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[6]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[7]")
ActiveSheet.PivotTables("NetRate22").PivotFields( _
"[Policy State Dimensions].[Effective Date].[Effective Day]").VisibleItemsList _
= Array("")
End Sub


I have been trying to solve this for a month now and haven't found the best resources in VBA to help with this solution.
Any help with this problem is appreciated. Thanks!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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