So I'm attempting to create a master data sheet that has a data cube in the form of a pivot table. Long term I will like to use other files to come back to this file and filter the cube and copy the data and paste it as values but in the short term I'm working on the aspect of the VBA code that filters the pivot table based on three cell selections. So far I'm able to filter for the location which is in cell K3, Now I'm trying to filter for the date range on the same pivot table. I have the following code:
Any insight on how I can get this vba to work for my date range?
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Range("K2:K3")) Is Nothing Then Exit Sub
Dim pt As PivotTable
Dim Field As Variant
Dim Field1 As Variant
Dim NewCat As String
Dim DateCat As String
Dim DateCat1 As String
Set pt = Worksheets("Fact Trans").PivotTables("PivotTable1")
Set Field = pt.PivotFields("[Locations].[Loc Name].[Location Name]")
Set Field1 = pt.PivotFields("[Date of Service].[Mth Year].[Mth Year]")
NewCat = Worksheets("Fact Trans").Range("K3").Value
DateCat = Worksheets("Fact Trans").Range("K4").Value
DateCat1 = Worksheets("Fact Trans").Range("K5").Value
With ActiveSheet.PivotTables("PivotTable1").PivotFields("[Locations].[Loc Name].[Location Name]")
.ClearAllFilters
.PivotFilters.Add Type:=xlCaptionEquals, Value1:=ActiveSheet.Range("K3").Value
.PivotFilters.Add Type:=xlDateBetween, Value1:=ActiveSheet.Range("K4").Text, Value2:=ActiveSheet.Range("K5").Text
End With
End Sub
Any insight on how I can get this vba to work for my date range?