I have a pivot tabel from excel 2003 that has weekday of month 1 through 23
I want to automate the update that today would be day 2 and tomorrow would be day 3
How can one get the pivot table tomorrow to show me the data without manually going into to the pivot table and unclicking day 2 and then clicking day 3
I am not sure I follow? Why are there only 23 days in a month?
If you are happy with the answer, please click the Star icon in the below left hand corner.
Good sites to start learning.
snb's VBA Help Files
Jerry Beaucaires Excel Assistant
J & R Excel Consultancy Services
How to post code correctly: Correct Code Posting
I am not sure if you want the days to coinside with the actually days of the calender, but I think your only way will be with VBA.
Try this,
Open the visual basic editor and paste this into the "ThisWorkbook" Module ,
Then go to "Insert" and Insert a new Module , don't insert a classModule, then paste the below code.Private Sub Workbook_Open() Run "PiviotRefresh" End Sub
Will automatically update your pivot chart to what ever day of the month is. Once you get past 23 however, it will do nothing. Which means that from the 24th of any month your pivot will not update. Tell me if that is what you were after?Sub PiviotRefresh() Dim pt As PivotTable ' On Error Resume Next Range("K1").FormulaR1C1 = "=TODAY( )" Range("L1").FormulaR1C1 = "=DAY(RC[-1])" Worksheets("Sheet1").Cells(5, 1).Value = Worksheets("Sheet1").Cells(1, 12).Value Set pt = ActiveSheet.PivotTables("PivotTable1") pt.RefreshTable Range("K1:L1").ClearContents End Sub
If you are happy with the answer, please click the Star icon in the below left hand corner.
Good sites to start learning.
snb's VBA Help Files
Jerry Beaucaires Excel Assistant
J & R Excel Consultancy Services
How to post code correctly: Correct Code Posting
Hi,
JapanDave,
Your code works properly but the previous selected day remain active.
Thanks for sharing
JapanDave Many thanks but as JPR kindly said it leaves the original day clicked as well
The answer to why a maximum of 23 days is purely because that is the maximum number of weekdays in a month as I am sure you figured out.
The data that I access is purely Monday through Friday and is stock market related
I have in essence created 3 pivot tables. 1. The Weekday of the Month - (1-23) 2, The Weekday - (Mon thru Frid) 3. The Day of the Year - (1-259 for the year 2011) as I wish to extract a probability scenario based on the seperate outcomes of interrogating the database/pivot tables
Many thanks to JapanDave and JPR73 for your input and help so far
Last edited by alleyb; 12-02-2011 at 09:22 AM. Reason: spelling
And as a additional factor to answer another part of your question
yes the pivot table should whether the number of weekdays in a month be 20 or 21 or 22 or on the rare occassion be 23 then on the new month we should revert back to day 1
Ok, This will deal with your problem about leaving the previous days number in.
But you have given a whole new set of variables to deal with. This set of code won't work if you are looking at the table "Just" on weekdays. I am going to have to think about this.Sub PiviotRefresh() Dim pt As PivotTable Dim ws As Worksheet Dim Day As Variant Application.ScreenUpdating = False Set ws = Worksheets("Sheet1") On Error Resume Next With ws .Cells(1, 11).FormulaR1C1 = "=TODAY( )" .Cells(1, 12).FormulaR1C1 = "=DAY(RC[-1])" .Cells(5, 1).Value = ws.Cells(1, 12).Value End With Day = ws.Cells(1, 12).Value Day = Day - 1 Set pt = ActiveSheet.PivotTables("PivotTable1") With ActiveSheet.PivotTables("PivotTable1").PivotFields("Day of Month") .PivotItems(Day).Visible = False End With pt.RefreshTable Range("K1:L1").ClearContents Application.ScreenUpdating = True End Sub
Last edited by JapanDave; 12-02-2011 at 09:15 PM.
If you are happy with the answer, please click the Star icon in the below left hand corner.
Good sites to start learning.
snb's VBA Help Files
Jerry Beaucaires Excel Assistant
J & R Excel Consultancy Services
How to post code correctly: Correct Code Posting
Ok, I figured it out for you. This code is tested and works. Upon opening it deletes the previous days value and adjust for each month and how many days it has in it and put that number in the pivot table accordingly. You are good to go until 2015
Code:
Edit: I had to revise the code. You are going to have to wait a day to test it, as it is working with todays date.Sub PiviotRefresh() Dim ws As Worksheet Dim ws1 As Worksheet Dim Day As Variant Dim pt As PivotTable Dim NDay As Variant On Error Resume Next Application.ScreenUpdating = False Set ws = Worksheets("Sheet2") Set ws1 = Worksheets("Sheet1") With ws If IsNumeric(ws.Cells(1, 6)) Then ws.Cells(3, 6).Value = ws.Cells(1, 6).Value End With Day = ws.Cells(2, 6).Value NDay = ws.Cells(3, 6) Set pt = ActiveSheet.PivotTables("PivotTable1") With ActiveSheet.PivotTables("PivotTable1").PivotFields("Day of Month") .PivotItems(Day).Visible = False .PivotItems(NDay).visble = True End With pt.RefreshTable Range("K1:L1").ClearContents Application.ScreenUpdating = True End Sub Sub Copy() Dim ws As Worksheet Set ws = Worksheets("Sheet2") ws.Cells(2, 6).Value = ws.Cells(1, 6).Value End Sub
If you can't get it to work on your actual file let me know. Cheers
Last edited by JapanDave; 12-04-2011 at 09:16 PM.
If you are happy with the answer, please click the Star icon in the below left hand corner.
Good sites to start learning.
snb's VBA Help Files
Jerry Beaucaires Excel Assistant
J & R Excel Consultancy Services
How to post code correctly: Correct Code Posting
JapanDave. Apologies re the length of time after your hard work on my issue. I have moved to the US now and beg for assistance. I have on several occasions downloaded the latest file you posted and cannot get it to update today for example should be day 20 and yet it gives me day 3 for today Jan 27
Any help would be appreciated
PS I have now loaded excel 2010 re my posting above
Sorry, that preivious code was bad code. I have re-done the code and this should not give you any problems.
Sub PiviotRefresh() Dim pItem As PivotItem Dim pvt As PivotTable Dim pvtF As PivotField Dim sItem As String Dim ws As Worksheet Application.ScreenUpdating = 0 Set pvt = Worksheets("Sheet1").PivotTables("PivotTable1") Set pvtF = pvt.PivotFields("Day of Month") Set ws = Sheets("Sheet2") sItem = ws.Cells(1, 6).Value For Each pItem In pvtF.PivotItems On Error Resume Next pItem.Delete On Error GoTo 0 Next pItem On Error GoTo InvalidFilter pvtF.PivotItems(sItem).Visible = True On Error GoTo 0 For Each pItem In pvtF.PivotItems If pItem = sItem Then pItem.Visible = True Else pItem.Visible = False End If Next pItem Application.ScreenUpdating = 1 Exit Sub InvalidFilter: MsgBox ("The filter """ & sItem & """ doesn't exist") End Sub
If you are happy with the answer, please click the Star icon in the below left hand corner.
Good sites to start learning.
snb's VBA Help Files
Jerry Beaucaires Excel Assistant
J & R Excel Consultancy Services
How to post code correctly: Correct Code Posting
Thankyou Thankyou JapanDave. I will await Monday but it seems to work. Now I just have to impliment it into my own spreadsheet
Might take me a few days as I work on the weekends trying to earn a living
Once again many thanks and thankyou for the very very speedy response after such a timelag
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks