Macro for Changing Pivot Tables Dates

Jar888

Board Regular
Joined
Jan 15, 2022
Messages
61
Office Version
  1. 2016
Platform
  1. Windows
Hi all! So, I've got a bit of a brainchild to a previous problem. Just wondering if there's any potential way that I can speed up the process of it changing the dates across my pivot tables? Any help would be great!

Thanks

VBA Code:
Sub PivotMaster()
' Macro for updating the dates in the delay pivot tables
' With no other Excel spreadsheets open: ~10sec
' With other Excel spreadsheets open: ~9min
Dim curmonth As String
Dim curmonth1S As String
Dim prevmonth As String
Dim prevmonth1S As String
Dim dateS As String
Dim yesterday As String
Dim yesterdayD As Date
Dim curmonth1D As Date
Dim prevmonth2D As Date
Dim d As Range
Dim k As Long

yesterday = DatePart("d", Range("I38")) & "-" & MonthName(DatePart("m", Range("I38")), True) 'Transforms yesterdays date into a "DD-MMM" format

curmonth = MonthName(DatePart("m", yesterday), True)
curmonth1S = "1/" & curmonth & "/" & DatePart("yyyy", yesterday) 'First day of the current month
curmonth1D = curmonth1S

prevmonth2D = curmonth1D - 1 'Last day of the previous month
prevmonth = MonthName(DatePart("m", prevmonth2D), True)
prevmonth1S = "1/" & prevmonth 'First day of previous month


'---------------Updating delay dates for single date pivot tables-------------------

    ActiveSheet.PivotTables("PivotTable3").PivotFields("Date").ClearAllFilters
    ActiveSheet.PivotTables("PivotTable3").PivotFields("Date").CurrentPage = _
        yesterday

    ActiveSheet.PivotTables("PivotTable4").PivotFields("Date").ClearAllFilters
    ActiveSheet.PivotTables("PivotTable4").PivotFields("Date").CurrentPage = _
        yesterday

'---------------Updating delay dates for multi date pivot tables--------------------
k = 1
Do Until k > DatePart("d", yesterday) 'Creates collection of dates from the first of the curent month until yesterday's date
dateS = k & "-" & curmonth

ActiveSheet.PivotTables("PivotTable14").PivotFields("Date").CurrentPage = _
        "(All)"
    ActiveSheet.PivotTables("PivotTable14").PivotFields("Date").PivotItems(dateS).Visible = True

    ActiveSheet.PivotTables("PivotTable15").PivotFields("Date").CurrentPage = _
        "(All)"
    ActiveSheet.PivotTables("PivotTable15").PivotFields("Date").PivotItems(dateS).Visible = True

k = k + 1

Loop

k = 1

'---------------De-select dates in the previous month if applicable-----------------
If curmonth = "Jan" Or DatePart("d", yesterday) = 1 Then
    Do Until k > DatePart("d", prevmonth2D)
    dateS = k & "-" & prevmonth
    
    ActiveSheet.PivotTables("PivotTable14").PivotFields("Date").CurrentPage = _
        "(All)"
    ActiveSheet.PivotTables("PivotTable14").PivotFields("Date").PivotItems(dateS).Visible = False
    
    ActiveSheet.PivotTables("PivotTable15").PivotFields("Date").CurrentPage = _
        "(All)"
    ActiveSheet.PivotTables("PivotTable15").PivotFields("Date").PivotItems(dateS).Visible = False
    
    k = k + 1
    Loop
Else

End If

ActiveWorkbook.RefreshAll

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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