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
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