+ Reply to Thread
Results 1 to 14 of 14

Changing The Dates On Pivot Table Filters Using VBA

  1. #1
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Changing The Dates On Pivot Table Filters Using VBA

    Hi,

    I have a Workbook with a lot of PivotTables on it.

    I am trying to change the date on the filter to todays date..

    Can someone guide me whats wrong with this code? I am unable to get it to work for some reason..

    Please Login or Register  to view this content.
    But, when I change it to this line, it suddenly works

    Please Login or Register  to view this content.
    I also have the Date in my underlying data returned by the Date function.

    This is the code generated by the Macro Recorder, but that does not work when done using a variable or even a hard coded value for that matter

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by NeedForExcel; 08-27-2015 at 07:00 AM.
    Cheers!
    Deep Dave

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Changing The Dates On Pivot Table Filters Using VBA

    It's expecting the date as text (not as a serial date value) with the same date format as used on the sheet.
    Try something like this. Change the date format to suit.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Changing The Dates On Pivot Table Filters Using VBA

    Hi,

    Thank you for the reply..

    I have tried that aswell, but does not seem to work. The dates on the file attached in Post #1 are in this format "d-mmm-yy"

    Please Login or Register  to view this content.
    Can you see the attachment once if possible?

    Or should I be using the PivotField property?
    Last edited by NeedForExcel; 08-27-2015 at 11:42 PM.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Changing The Dates On Pivot Table Filters Using VBA

    My older version of Excel won't convert your version of pivot tables. So I cannot use your pivot table. I can make a new pivot table with your data and pivot on the dates with VBA. Note that your data does not have a date for 28-Aug-15. So you can not pivot on today's date.

  5. #5
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Changing The Dates On Pivot Table Filters Using VBA

    Actually while trying I changed the dates in my Data to 28th Aug 15.

    Can you attach the file created by you along with the working code?

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Changing The Dates On Pivot Table Filters Using VBA

    Quote Originally Posted by NeedForExcel View Post
    Actually while trying I changed the dates in my Data to 28th Aug 15.
    Did you create a new pivot table after changing the date in the data? Changing the data and only refreshing does not add a new date to the pivot field dropdown.
    Attached Files Attached Files

  7. #7
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Changing The Dates On Pivot Table Filters Using VBA

    Still returns the same error

  8. #8
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Changing The Dates On Pivot Table Filters Using VBA

    Any help please? Someone?

  9. #9
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: Changing The Dates On Pivot Table Filters Using VBA

    Please try this approach to see if this works for you.....
    Please Login or Register  to view this content.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  10. #10
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Changing The Dates On Pivot Table Filters Using VBA

    It works, but it possesses pretty slowly.

    I came up with this, which is working extremely fast, however, the date in Range A2, needs to be formatted exactly in the format thats on the base data.

    Please Login or Register  to view this content.

  11. #11
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: Changing The Dates On Pivot Table Filters Using VBA

    But both the approach will be failed if you don't have the record for the mentioned date.

  12. #12
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Changing The Dates On Pivot Table Filters Using VBA

    True that!

    But fortunately, the Drop Down also has a query that runs to get the distinct dates. So it is not possible to have a date that's not there on the Pivot..

    The approach is likely to fail, if there are more filters/No Filters/Different Filters on the Pivot Table, so I updated the code to this

    Please Login or Register  to view this content.

  13. #13
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,165

    Re: Changing The Dates On Pivot Table Filters Using VBA

    Hi Need4XL,

    A few years ago I was heavy into PivotItems and making multiple Pivot Tables have the same filters if ANY of them changed. See the attached which might have code to help you. If you change the filter of any of the 3 tables, the others will also change. See if this helps answer this question. Part of what I learned is making the Pivot Filter a multi filter (with the boxes to the left) creates a little different macro recorded code than what you have.
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  14. #14
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Changing The Dates On Pivot Table Filters Using VBA

    Hi,

    It is 90% of what I was looking for.. Only my selection value came from a Drop Down List.

    What I noticed is, In case of regular Text values, it is not as tuff, however when it get to Dates, it becomes a headache.

    I also noticed, your date needs to be formatted in exactly the same way as on the Pivot Report Filter (Even though it is actual Date and Not Text)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 07-16-2015, 05:46 AM
  2. Marco for changing numerous pivot chart filters at once
    By Nick Simo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2012, 12:09 PM
  3. [SOLVED] Changing dates in a Pivot Table using a Macro
    By peter_caswell in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-27-2012, 02:58 AM
  4. Changing the Pivot Table Filters based on Multiple Cells
    By dbnhc8 in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 08-28-2012, 02:37 PM
  5. Replies: 1
    Last Post: 02-22-2012, 12:21 PM
  6. VBA Code - Changing filters of several pivot tables based on cell values
    By kriskrispies in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-14-2010, 08:49 PM
  7. Pivot Table Filters always show as dates
    By smninos in forum Excel General
    Replies: 2
    Last Post: 12-03-2009, 03:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1