How to show most recent date in Pivot Table filter field ?

AngelicaWan

New Member
Joined
Aug 20, 2015
Messages
8
I just encounter this issue and want to find a solution to auto filter just the most recent date in Pivot Table via VBA.

Like I want to set "TIME" column as filter field in Pivot Table. I searched online and modified a little bit to see whether it could work. Here are the codes:

Module1:
Sub MaxDatePivot()
Dim pfiPivFldItem As PivotItem
Dim PvtTbl As PivotTable
Dim dtmDate As Date
With ThisWorkbook.Sheets("Sheet1").PivotTables(1)
.ClearAllFilters
With .RowRange
dtmDate = Evaluate("MAX(IF(ISNUMBER(" & .Address(0, 0) & ")," & .Address(0, 0) & ",))")
End With
For Each pfiPivFldItem In .PivotFields("TIME").PivotItems
If pfiPivFldItem.Value = "(blank)" Then
pfiPivFldItem.Visible = False
Else
pfiPivFldItem.Visible = CDate(pfiPivFldItem.Value) = CLng(dtmDate)
End If
Next pfiPivFldItem
PvtTbl.PivotFields("TIME").PivotFilters.Add Type:=xlAfterOrEqualTo, Value1:=pfiPivFldItem
End With
End Sub


ThisWorkbook:
Private Sub Workbook_Open()
MaxDatePivot
End Sub


However, it showed me the red part is "Type Mismatch".

Is it possible to make it happen? How to modify or write codes to realize it? Thanks!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi,

You could test ...
Code:
With ActiveSheet.PivotTables("PivotTable1")
      On Error Resume Next
        .RefreshTable
        .PivotFields("TIME").AutoSort xlDescending, "TIME"
      On Error GoTo 0
    End With

HTH
 
Upvote 0
Hi James,

Thank you so much! You give me an great idea to show other side of vision to solve this problem. And I modified a little bit and it finally works! Good news in this morning!

I put my code below:
Code:
With ActiveSheet.PivotTables("PivotTable1")
      On Error Resume Next
        .RefreshTable
        .PivotFields("TIME").AutoSort xlDescending, "TIME"
      On Error GoTo 0
    For i = 1 To .PivotFields("TIME").PivotItems.Count
        If .PivotFields("TIME").PivotItems(i) <> "True" And _
           .PivotFields("TIME").PivotItems(i) <> "False" And _
           .PivotFields("TIME").PivotItems(i) <> "(blank)" Then
            If (.PivotFields("TIME").PivotItems(1) > .PivotFields("TIME").PivotItems(i + 1)) Then
                .PivotFields("TIME").PivotItems(1).Visible = True
                .PivotFields("TIME").CurrentPage = .PivotFields("TIME").PivotItems(1).Value
            End If
        End If
    Next
End With

Happy Friday!


Hi,

You could test ...
Code:
With ActiveSheet.PivotTables("PivotTable1")
      On Error Resume Next
        .RefreshTable
        .PivotFields("TIME").AutoSort xlDescending, "TIME"
      On Error GoTo 0
    End With

HTH
 
Last edited:
Upvote 0
Hi Angelica,

Glad you could fix your problem ...;)

Thanks ... for your thanks ...
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,947
Members
449,480
Latest member
yesitisasport

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top