Pergunta

I have a table on a worksheet that contains a Date column. I have created a Pivot Table/Chart on a new worksheet. I want to be able to filter the data in the chart being displayed by the last 14 days (Always relative to the current date).

I have the same problem as this user:filtering an Excel pivot table to show the last 24 hours data

Except I can't add a column to the source Data and I need it within 14 days instead of 24 hours. There must be built-in way to do this or can I do this in VBA?

Thanks

Foi útil?

Solução

You can filter by date, of course, but without VBA I think it has to be manual. Here's a VBA routine that sets it to the last 14 days, including today:

Sub FilterPivotByDate()
Dim pvt As Excel.PivotTable
Dim DaysToShow As Long
Dim DateString As String

Set pvt = ActiveSheet.PivotTables(1)
DaysToShow = 114
DateString = Format(Date - (DaysToShow - 1), "m/d/yyyy")
With pvt.PivotFields("date")
    .ClearAllFilters
    .PivotFilters.Add Type:=xlAfterOrEqualTo, Value1:=DateString
End With
End Sub
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top