DateBetween, change from a date to a value from a cell

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello all,

Can someone change the code below so that...

Value 1 = Value from the date in cell "AA2" in the "C" worksheet
Value 2 = Value from the date in cell "AA4" in the "C" worksheet

My "laymens" way of illustrating it would be like this...
PivotFilters.Add2 Type:=xlDateBetween, Value1:=C!AA2, Value2:=C!AA4 <this is what I need code for

VBA Code:
Sheets("Chart 2 MCDB").Select
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveSheet.PivotTables("PivotTable1").PivotFields("LCD"). _
        ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("LCD"). _
        PivotFilters.Add2 Type:=xlDateBetween, Value1:="1/1/2020", Value2:="12/31/2020" '<<<this line

Thanks much!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Perhaps
VBA Code:
PivotFilters.Add2 Type:=xlDateBetween, Value1:=WorkSheets("C").Range("AA2").Value, Value2:=WorkSheets("C").Range("AA4").Value
 
Upvote 0
Hi Jason,
Thanks for that code, but I got the error below...
Run-time error '1004':
Application-defined or object-defined error

Any suggestions?
 
Upvote 0
Try this instead, using .Value might be picking up the date serial instead of the actual date as a string.
VBA Code:
PivotFilters.Add2 Type:=xlDateBetween, Value1:=WorkSheets("C").Range("AA2").Text, Value2:=WorkSheets("C").Range("AA4").Text
 
Upvote 0
Did you just replace the last line of your old code with it? The 'Object required' error suggests that the line above it is missing or has no underscore at the end.
The full code for clarity, although you shouldn't need the first 2 lines.
VBA Code:
Sheets("Chart 2 MCDB").Select
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveSheet.PivotTables("PivotTable1").PivotFields("LCD"). _
        ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("LCD"). _
        PivotFilters.Add2 Type:=xlDateBetween, Value1:=WorkSheets("C").Range("AA2").Text, Value2:=WorkSheets("C").Range("AA4").Text
 
Upvote 0
Solution
OMG, that is too funny, I just tried that and yes, you are correct!
I forgot the "clear filter" line!

THANKS SO MUCH, THIS IS PERFECT!
 
Upvote 0
Simplified version (not tested but should work).
VBA Code:
With WorkSheets("Chart 2 MCDB").PivotTables("PivotTable1").PivotFields("LCD")
        .ClearAllFilters
        .PivotFields("LCD").PivotFilters.Add2 Type:=xlDateBetween, Value1:=WorkSheets("C").Range("AA2").Text, Value2:=WorkSheets("C").Range("AA4").Text
End With
It is rarely necessary to select anything, using With blocks allows you to bypass that and make things run a lot quicker.
Selecting the chart didn't appear to be serving any purpose so I've left that out.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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