Multiple Pivot Filters Set By Cell Value

RJW2809

New Member
Joined
Jun 17, 2018
Messages
2
Hi,

Bit of a novice here working in the dark but I'm trying to set four pivot filter values in a single Pivot Table to what are changing cell values based on the cell that is selected in the work sheet.

My code is below and I get the error message "Run Time Error '1004' Application-defined or object-defined error".

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
'Part 1 (works fine) this identifes which two fields are selected and sets two corrisponding filter values based on the cell that is selected in the range and thus sets the values that are to be referenced in part 3 through a seperate calculation in the worksheet.
If Not Intersect(Target, Range("AA16:AQ32")) Is Nothing Then
Worksheets("FC_LocationExceptions").Range("O14").Value = Selection.Offset(0, 31).Value
Worksheets("FC_LocationExceptions").Range("O15").Value = Selection.Offset(0, 51).Value
Worksheets("FC_LocationExceptions").Range("P14").Value = Range("U12")
Worksheets("FC_LocationExceptions").Range("P15").Value = Range("U13")


'Part 2 (presume works as no error here) clears the pivot table filters
With Worksheets("FC_LocationExceptions").PivotTables("LocationExceptions")
.PivotFields("DailyBiasGroup").ClearAllFilters
.PivotFields("ReturnQtyGroup").ClearAllFilters
.PivotFields("ReturnValueGroup").ClearAllFilters
.PivotFields("ZeroReturnGroup").ClearAllFilters


'Part 3 (where I'm getting an error) sets the 4 filter values to the results of the calculations from part 1.
'Any two of the fields will be set to (All) and the other two will have numeric values resulting from part 1.
'Which two are set to (All) and which have values will be variable depending on other selections in the sheet
.PivotFields("DailyBiasGroup").PivotFilters.Add Type:=xlCaptionEquals, Value1:=[V3].Value
.PivotFields("ReturnQtyGroup").PivotFilters.Add Type:=xlCaptionEquals, Value1:=[V4].Value
.PivotFields("ReturnValueGroup").PivotFilters.Add Type:=xlCaptionEquals, Value1:=[V5].Value
.PivotFields("ZeroReturnGroup").PivotFilters.Add Type:=xlCaptionEqualss, Value1:=[V6].Value
'The process then repeats with a different cell range but with the same intended results, but in the below two there would be three fields with a value of (All)
End With


End If
If Selection.Count = 1 Then
If Not Intersect(Target, Range("AA14:AQ14")) Is Nothing Then
Worksheets("FC_LocationExceptions").Range("O14").Value = Selection.Offset(0, 31).Value
Worksheets("FC_LocationExceptions").Range("O15").Value = Selection.Offset(0, 31).Value
Worksheets("FC_LocationExceptions").Range("P14").Value = Range("U12")
Worksheets("FC_LocationExceptions").Range("P15").Value = Range("U12")


With Worksheets("FC_LocationExceptions").PivotTables("LocationExceptions")
.PivotFields("DailyBiasGroup").ClearAllFilters
.PivotFields("ReturnQtyGroup").ClearAllFilters
.PivotFields("ReturnValueGroup").ClearAllFilters
.PivotFields("ZeroReturnGroup").ClearAllFilters


.PivotFields("DailyBiasGroup").PivotFilters.Add Type:=xlCaptionEquals, Value1:=[V3].Value
.PivotFields("ReturnQtyGroup").PivotFilters.Add Type:=xlCaptionEquals, Value1:=[V4].Value
.PivotFields("ReturnValueGroup").PivotFilters.Add Type:=xlCaptionEquals, Value1:=[V5].Value
.PivotFields("ZeroReturnGroup").PivotFilters.Add Type:=xlCaptionEqualss, Value1:=[V6].Value
End With


End If
If Selection.Count = 1 Then
If Not Intersect(Target, Range("Y16:Y32")) Is Nothing Then
Worksheets("FC_LocationExceptions").Range("O14").Value = Selection.Offset(0, 51).Value
Worksheets("FC_LocationExceptions").Range("O15").Value = Selection.Offset(0, 51).Value
Worksheets("FC_LocationExceptions").Range("P14").Value = Range("U13")
Worksheets("FC_LocationExceptions").Range("P15").Value = Range("U13")


With Worksheets("FC_LocationExceptions").PivotTables("LocationExceptions")
.PivotFields("DailyBiasGroup").ClearAllFilters
.PivotFields("ReturnQtyGroup").ClearAllFilters
.PivotFields("ReturnValueGroup").ClearAllFilters
.PivotFields("ZeroReturnGroup").ClearAllFilters


.PivotFields("DailyBiasGroup").PivotFilters.Add Type:=xlCaptionEquals, Value1:=[V3].Value
.PivotFields("ReturnQtyGroup").PivotFilters.Add Type:=xlCaptionEquals, Value1:=[V4].Value
.PivotFields("ReturnValueGroup").PivotFilters.Add Type:=xlCaptionEquals, Value1:=[V5].Value
.PivotFields("ZeroReturnGroup").PivotFilters.Add Type:=xlCaptionEqualss, Value1:=[V6].Value
End With


End If
End If
End If
End If
End Sub


Any help would be greatly appreciated, many thanks in advance

RJW
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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