+ Reply to Thread
Results 1 to 4 of 4

Filtering a Pivot by Date Range via VBA

  1. #1
    Registered User
    Join Date
    02-11-2013
    Location
    North
    MS-Off Ver
    Excel 2010
    Posts
    11

    Filtering a Pivot by Date Range via VBA

    Hi,

    I am trying to filter a pivot table using VBA. Specifically a date range. So I want a user to enter a date from and to on a excel sheet and the pivot will then update based on them cells, however I have been unable to thus far.


    I have attached an example of my problem.


    What I have been able to do so far is filter by 'date' when it is added in as a 'row label' by:

    Sub UpdatePivot()

    Dim PvtTbl As PivotTable
    Set PvtTbl = Worksheets("Worksheet").PivotTables("PivotTable1")
    PvtTbl.ClearAllFilters
    PvtTbl.PivotFields("Date").PivotFilters.Add Type:=xlDateBetween, Value1:=CLng(Range("Datefrom").Value), Value2:=CLng(Range("Dateto").Value)

    End Sub

    -This doesn't work when I have 'date' just in my filter and not row label, as I do not want to do this.

    -Please could someone help, I've been trying for a while to fix this now.ExampleofPivot.xlsx

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Filtering a Pivot by Date Range via VBA

    I put the start date in cell H1 and end date in cell J1

    this code turns all the dates on then turn off the ones that aren't in the date range....


    Please Login or Register  to view this content.
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Registered User
    Join Date
    02-11-2013
    Location
    North
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Filtering a Pivot by Date Range via VBA

    Thanks. Done the trick.

    A different way to what I was thinking as my original piece of code was pretty close to what I need thought it would have just required some manipulation.

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

    Re: Filtering a Pivot by Date Range via VBA

    Dang. I was really hoping this bit of code was going to work.

    All I get is

    "unable to set the visible property of the pivotitem class"

+ 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. Pivot Table Report Filtering by Date
    By Designs in forum Excel General
    Replies: 3
    Last Post: 02-11-2016, 08:23 AM
  2. date filtering using pivot or combo
    By NoahJEck in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 07-21-2015, 04:49 PM
  3. Filtering based on date in Pivot Table
    By jswalemail in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 03-10-2014, 09:44 PM
  4. [SOLVED] Filtering on date range, but capturing items that 'straddle' the range
    By Kite3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-30-2013, 05:38 AM
  5. Filtering a pivot by time range
    By koootzz in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 05-05-2013, 01:26 PM
  6. Filtering Minimum Date Field in Pivot Table
    By investmentbanker in forum Excel General
    Replies: 1
    Last Post: 07-13-2012, 05:38 PM
  7. Pivot table date filtering question?
    By s_zabihi in forum Excel General
    Replies: 10
    Last Post: 12-06-2011, 12:08 PM

Tags for this Thread

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