VBA to Filter Pivot Table based on Start/End Date in Cells


Board Regular
I have a pivot table named PivotTable2 on a Worksheet named Three Pivots. One of the FILTERS is named DATE_DUE. I need the filter to have only the last five days checked. Currently I have the start date in cell C5 and the end date in cell C1 (If I don't need the cell reference, that is okay) Today is 10/04/2019 so I would need any date between and including 09/29/2019 through 10/03/2019 checked.

Thank you


New Member

I've got some code, used before to do a similar thing:

Private Sub Filter_Pivot(xP As Variant, xPField As String)    Dim ws As Worksheet
    Dim xPElement  As Variant
    Dim xPTable As PivotTable
    Dim xPFile As PivotField
    Dim startDate As Date
    Dim endDate As Date
    Set ws = ThisWorkbook.Worksheets("*")
    startDate = ws.Range("C1").Value
    endDate = ws.Range("C5").Value

    With Worksheets("*")
        For Each xPElement In xP
            Set xPTable = .PivotTables(xPElement)
            Set xPFile = xPTable.PivotFields(xPField)
            xPFile.PivotFilters.Add Type:=xlDateBetween, Value1:=CLng(startDate), Value2:=CLng(endDate)
        Next xPElement
    End With
End Sub
I have my dates field in the 'Rows' field, collapsed under the other dimension i am filtering by.

Maybe someone else can help you adapt it to your project.



Board Regular
Dtex20 - When I copied and pasted your code, the first line is red and generates a Compile error: Syntax error

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Macro to copy values across rows and transposing them and add the user id
    [FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Hi,[/COLOR][/SIZE][/FONT] [FONT=Times New...