VBA filter Pivot table based on a specific cell value

clomah

New Member
Joined
Jun 9, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello!

Hoping someone can help me as I've spent the past 5 hours trying different methods with no luck.

I've recorded the below macro and it works, however I'm trying to replace [2022-05-01T00:00:00] and [2023-05-01T00:00:00] with a reference to cells A1 and A2 (based on the month selected)

VBA Code:
Sub PivotMonth()

    ActiveSheet.PivotTables("PivotTable1").PivotFields( _
        "[XXX Calendar].[XXX Fiscal Month].[XXX Fiscal Month]").VisibleItemsList = _
        Array("[XXX Calendar].[XXX Fiscal Month].&[2022-05-01T00:00:00]", _
        "[XXX Calendar].[XXX Fiscal Month].&[2023-05-01T00:00:00]")
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Month!

I'm guessing it would have to be in that same format? So I'm having a formula to translate to:

[2022-06-01T00:00:00] A1
[2023-06-01T00:00:00] A2

If that makes sense.

Thank you!
 
Upvote 0
Does it contain a value like this...

VBA Code:
6

or

VBA Code:
Jun

or

VBA Code:
June

???
 
Upvote 0
In that case, try the following...

VBA Code:
Array("[XXX Calendar].[XXX Fiscal Month].&[2022-" & Format(Range("A1").Value & " 1", "mm") & "-01T00:00:00]", _
        "[XXX Calendar].[XXX Fiscal Month].&[2023-" & Format(Range("A2").Value & " 1", "mm") & "-01T00:00:00]")

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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