Herbalgiraffe
New Member
- Joined
- Feb 22, 2020
- Messages
- 16
- Office Version
- 365
- Platform
- Windows
Hey Y'all,
I am brand new to the forum and looking for some wisdom and advice as an ambitious new student of VBA. I am currently trying to compress the process of updating a sensitive report that is being used at my work which in total holds 29 pivot tables for all sorts of things across 15 worksheets. The pivot tables are formed from a cube of data that all share a common filter of "posting date". after failing to tie a slicer to all of them I turned to the little code I know currently. The issue is that some of the reports need to be updated as year to date, while others need to be only for the previous month, so my current thought is to declare the worksheets into two groups which are then run through two different loops of unfiltering and refiltering accordingly. Below is what I currently have, but the other challenge is that testing the filtering process by recording myself, it shows up in a format I have never seen before, which is the second chunk of code. Any wisdom you can give on how to make the two dance together would make my life a heck of a lot easier. Thanks in advance!
I am brand new to the forum and looking for some wisdom and advice as an ambitious new student of VBA. I am currently trying to compress the process of updating a sensitive report that is being used at my work which in total holds 29 pivot tables for all sorts of things across 15 worksheets. The pivot tables are formed from a cube of data that all share a common filter of "posting date". after failing to tie a slicer to all of them I turned to the little code I know currently. The issue is that some of the reports need to be updated as year to date, while others need to be only for the previous month, so my current thought is to declare the worksheets into two groups which are then run through two different loops of unfiltering and refiltering accordingly. Below is what I currently have, but the other challenge is that testing the filtering process by recording myself, it shows up in a format I have never seen before, which is the second chunk of code. Any wisdom you can give on how to make the two dance together would make my life a heck of a lot easier. Thanks in advance!
VBA Code:
Sub RefreshAllPivots()
Dim wks As Worksheet
Dim pt As PivotTable
Dim YTD As Variant
Dim MTD As Variant
YTD = ActiveWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
MTD = ActiveWorkbook.Sheets(Array("Sheet4", "Sheet5", "Sheet6"))
For Each wks In YTD
For Each pt In wks.PivotTables
pt.PivotFields("Date").ClearAllFilters
pt.PivotFields("Date").PivotFilters.Add Type:=xlDateBetween, _
Value1:=CLng((Range("01/01/2020").Value)), Value2:=CLng((Range("Today's Date").Value))
Next pt
Next wks
.Activate
For Each wks In MTD
For Each pt In wks.PivotTables
pt.PivotFields("Date").ClearAllFilters
pt.PivotFields("Date").PivotFilters.Add Type:=xlDateBetween, _
Value1:=CLng((Range("First Date of last month").Value)), Value2:=CLng((Range("Last Date of last month").Value))
Next pt
Next wks
.Activate
End Sub
VBA Code:
Sub Month1()
'
' Month1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+Q
'
ActiveSheet.PivotTables("PivotTable3").PivotFields("[Posting Date].[Date YQMD].[Year]").VisibleItemsList = Array("")
ActiveSheet.PivotTables("PivotTable3").PivotFields("[Posting Date].[Date YQMD].[Quarter]").VisibleItemsList = Array("")
ActiveSheet.PivotTables("PivotTable3").PivotFields("[Posting Date].[Date YQMD].[Month]").VisibleItemsList = Array("[Posting Date].[Date YQMD].[Month].&[2]&[2019]", "[Posting Date].[Date YQMD].[Month].&[2]&[2020]")
ActiveSheet.PivotTables("PivotTable3").PivotFields("[Posting Date].[Date YQMD].[Day]").VisibleItemsList = Array("")