Pivot filter on date with xlAfterOrEqualTo and xlAfter and others generate error

Invisibleman

New Member
Joined
Sep 16, 2015
Messages
13
Dear all,

I try to set the date filter on the Pivot thru VBA. But if I do try with the date, this is generating an error, where I don't know why/how to solve.

I have extra set the cell value to a date in the code, but this also doesn't help.
Date.jpg

My code is;

VBA Code:
Private Sub box47_6_SelectCrypto_Change()
Dim startdate As Date
startdate = New_Chart_DailyCrypto.Range("K1").Value
   With New_Pivot_DailyExchange.PivotTables("Pivottable1").PivotFields("Crypto")
        .ClearAllFilters
        .PivotFilters.Add Type:=xlCaptionContains, Value1:=box47_6_SelectCrypto.Value
        .PivotFilters.Add Type:=xlAfter, Value1:=startdate
End With

End Sub

And the error I am getting is;
Error.jpg


But to be honest, when searching the web, I don't see what I am doing wrong.
I also did try, for testing, to only filter on the xlafter, so disable the xlCaptionContains, but still I am receiving the error.

If you can help, that would be very appriciated,

Regards,
Hans
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Dear all,

I think found out why the error did occur.

It change the code so, that the clearallfilters is set per field.

However, after this I received the problems with the source date (DD/MM/YYYY format) when got the 13/01/2024, it did say invalid date.
So on the internet I then found to use the CLng(Startdate), which indeed also solved this problem.

If I can improve anything then please let me know?
Below the new code I have, this also to help others, running into the same issues.

VBA Code:
Private Sub box47_6_SelectCrypto_Change()

Dim startdate As Date

startdate = New_Chart_DailyCrypto.Range("K1").Value2

   With New_Pivot_DailyExchange.PivotTables("Pivottable1")
        .PivotFields("Crypto").ClearAllFilters
        .PivotFields("Crypto").PivotFilters.Add Type:=xlCaptionContains, Value1:=box47_6_SelectCrypto.Value
        .PivotFields("Start Date").ClearAllFilters
        .PivotFields("Start Date").PivotFilters.Add Type:=xlAfter, Value1:=CLng(startdate)
   End With

End Sub

Regards,
Hans
 
Upvote 0
Solution

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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