+ Reply to Thread
Results 1 to 14 of 14

Dynamic VBA code needed to filter pivot based on 2 cells

  1. #1
    Registered User
    Join Date
    01-03-2016
    Location
    Wolverhampton, england
    MS-Off Ver
    Excel
    Posts
    9

    Cool Dynamic VBA code needed to filter pivot based on 2 cells

    My start date is in cell C1.
    My end date is in cell D1.

    The field I want to adjust is

    PivotTables("PivotTable1").PivotFields("Weekending")

    I want it to show only the dates between the 2 cells. The date ranges in the field will always be different.

    I am really struggling with this because whatever code I write I keep getting errors back. There must be a simple solution to this that is utterly evading me so I have come here for help.

    If anyone could provide the code for this I would be incredibly grateful.

    Ryan

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    assuming your cells are C1,D1 and you are using excel 2010+ (not sure what year version this xldatebetween came on board...maybe 2007)

    Please Login or Register  to view this content.
    Last edited by humdingaling; 01-03-2016 at 10:43 PM. Reason: updated cell reference...
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Registered User
    Join Date
    01-03-2016
    Location
    Wolverhampton, england
    MS-Off Ver
    Excel
    Posts
    9

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    Thank you for replying.

    I am using 2007.

    I have put that code in and I get "Application-defined or object-defined error".

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    just checked
    should work with 2007
    https://msdn.microsoft.com/en-us/lib...ffice.12).aspx

    if the filter ON before you ran the code?
    you need to clear filter before running

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-03-2016
    Location
    Wolverhampton, england
    MS-Off Ver
    Excel
    Posts
    9

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    This is a pivot table rather than an auto-filter list thing.

    I get the same error.

    .PivotFilters.Add Type:=xlDateBetween, Value1:=Format(Range("C1"), "dd/mm/yy"), Value2:=Format(Range("D1"), "dd/mm/yy")

    Highlights the above in yellow. I made a dummy attachment with basic data but I dont see anywhere to put attachments on here.

  6. #6
    Registered User
    Join Date
    01-03-2016
    Location
    Wolverhampton, england
    MS-Off Ver
    Excel
    Posts
    9

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    I think I just attached.
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    not sure what you mean about autofilter

    i inserted the code into your sample file

    having no issues with it
    just changed the pivot table name to pivottable1
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    01-03-2016
    Location
    Wolverhampton, england
    MS-Off Ver
    Excel
    Posts
    9

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    I know where my issue lies now. Your code is definitely working with the dummy file. My only issue is the main excel file I am using has the filter for "between dates" greyed out for some reason. I need to figure out how to enable the between dates on my file and then I think I am away. However that is now proving elusive lol

  9. #9
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    double check on your original data source

    if one check is not a "proper" date then this option will not work

    see post #7
    http://www.ozgrid.com/forum/showthread.php?t=157361

  10. #10
    Registered User
    Join Date
    01-03-2016
    Location
    Wolverhampton, england
    MS-Off Ver
    Excel
    Posts
    9

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    They are most definitely all dates.

  11. #11
    Registered User
    Join Date
    01-03-2016
    Location
    Wolverhampton, england
    MS-Off Ver
    Excel
    Posts
    9

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    Image1.jpg

    Cant add photo for some reason so here it is as attachment.

  12. #12
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    grasping at straws without being able to see the file
    can you copy the data into a new sheet and create a pivot off that and see if that works then?

    if it doesnt then you know its the data

  13. #13
    Registered User
    Join Date
    01-03-2016
    Location
    Wolverhampton, england
    MS-Off Ver
    Excel
    Posts
    9

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    That is basically what I did with the dummy file and it worked. I need to be able to filter within this file and cant for the life of me figure out why the option is grayed out.

  14. #14
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Dynamic VBA code needed to filter pivot based on 2 cells

    its definitely something to do with your data set then
    maybe a few things you can try is changing in pivot table options
    under totals and filters > check Multiple Filter on
    under data > number of items to retain in field >> set to none

    with the original data source
    try conditional format the whole column for dates
    use formula = istext(whatever the first cell is) then highlight fill-red

    post #2
    http://www.mrexcel.com/forum/excel-q...vot-table.html

    as you already know
    its very hard to trouble shoot pivot tables without the actual table itself
    as it will work in one instance and not the other...very finicky

+ 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. How to filter a pivot table in vba (Dynamic)
    By fluffyvampirekitten in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-16-2015, 05:24 AM
  2. Dynamic filter on pivot table?
    By Cleland in forum Excel General
    Replies: 0
    Last Post: 10-23-2014, 07:50 AM
  3. Filter in Pivot based on cells
    By olwy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-02-2013, 06:51 AM
  4. vba code to filter pivot table column labels based on cell vaule
    By nailler167 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-31-2013, 09:12 PM
  5. [SOLVED] code to filter a column based on the value of two cells
    By Abhi1687 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-15-2013, 12:29 PM
  6. Daily Pivot Filter based on Date range specified in cells
    By swicked in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-02-2012, 01:23 AM
  7. Change Pivot table Filter Based on Cell Value *Multiple Filter items* Possible?
    By Flydd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-27-2012, 06:57 AM

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