Created
November 14, 2012 14:50
-
-
Save ronaldb/4072549 to your computer and use it in GitHub Desktop.
Macro to update date on two pivot tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub ChangeDate() | |
' | |
' ChangeDate Macro | |
' Update the date in both date boxes to same date (yesterday) | |
' Just a change for change's sake | |
' | |
' Keyboard Shortcut: Ctrl+d | |
' | |
Dim vcDateName As String | |
Dim vcDateYear As String | |
Dim vcDateQuarter As String | |
Dim vcDateMonth As String | |
Dim vcDateTime As String | |
Dim vtTheDate As Date | |
' Get Yesterday's date | |
vtTheDate = Date - 1 | |
' Format the various necessary fields | |
vcDateName = Format(vtTheDate, "yyyymmdd") | |
vcDateYear = Format(vtTheDate, "yyyy") | |
vcDateMonth = Format(vtTheDate, "m") | |
vcDateQuarter = Format(DatePart("q", vtTheDate) + 1, "d") | |
vcDateTime = Format(vtTheDate, "yyyy-mm-dd") | |
ActiveSheet.PivotTables("PivotTable1").PivotFields( _ | |
"[Calendar Filter].[Calendar Year - Quarter - Month - Date].[Calendar Year]"). _ | |
ClearAllFilters | |
ActiveSheet.PivotTables("PivotTable1").PivotFields( _ | |
"[Calendar Filter].[Calendar Year - Quarter - Month - Date].[Calendar Year]"). _ | |
CurrentPageName = _ | |
"[Calendar Filter].[Calendar Year - Quarter - Month - Date].[Calendar Year].&[" + vcDateYear + "].&[" + vcDateQuarter + "].&[" + vcDateMonth + "].&[" + vcDateTime + "T00:00:00]" | |
ActiveSheet.PivotTables("PivotTable2").PivotFields( _ | |
"[Transaction Date].[Calendar Year Qtr Month].[Calendar Year]").ClearAllFilters | |
ActiveSheet.PivotTables("PivotTable2").PivotFields( _ | |
"[Transaction Date].[Calendar Year Qtr Month].[Calendar Year]"). _ | |
CurrentPageName = _ | |
"[Transaction Date].[Calendar Year Qtr Month].[Date].&[" + vcDateName + "]" | |
Cells.Select | |
Selection.Columns.AutoFit | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment