hawk771960
New Member
- Joined
- Jun 23, 2015
- Messages
- 10
I could use some help with some error trapping for the following VBA code. This code very nicely controls 2 pivot tables filter values from a single cell value on a worksheet. The problem occurs when the value does not occur in the filter. I could use an elegant way to simply tell the user that the value does not exist for one or both of the Pivot Tables.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Intersect(Target, Worksheets("Lookup").Range("A2")) Is Nothing Then Exit Sub
Dim pt1 As PivotTable
Dim pt2 As PivotTable
Dim Field1 As PivotField
Dim Field2 As PivotField
Dim NewCat1 As String
Dim NewCat2 As String
Set pt1 = Worksheets("Lookup").PivotTables("PTProd")
Set Field1 = pt1.PivotFields("Material Number End")
NewCat1 = Worksheets("Lookup").Range("A2").Value
Set pt2 = Worksheets("Lookup").PivotTables("PTClaim")
Set Field2 = pt2.PivotFields("Material")
NewCat2 = Worksheets("Lookup").Range("A2").Value
With pt
Field1.ClearAllFilters
Field1.CurrentPage = NewCat1
pt1.RefreshTable
Field2.ClearAllFilters
Field2.CurrentPage = NewCat2
pt2.RefreshTable
End With
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Intersect(Target, Worksheets("Lookup").Range("A2")) Is Nothing Then Exit Sub
Dim pt1 As PivotTable
Dim pt2 As PivotTable
Dim Field1 As PivotField
Dim Field2 As PivotField
Dim NewCat1 As String
Dim NewCat2 As String
Set pt1 = Worksheets("Lookup").PivotTables("PTProd")
Set Field1 = pt1.PivotFields("Material Number End")
NewCat1 = Worksheets("Lookup").Range("A2").Value
Set pt2 = Worksheets("Lookup").PivotTables("PTClaim")
Set Field2 = pt2.PivotFields("Material")
NewCat2 = Worksheets("Lookup").Range("A2").Value
With pt
Field1.ClearAllFilters
Field1.CurrentPage = NewCat1
pt1.RefreshTable
Field2.ClearAllFilters
Field2.CurrentPage = NewCat2
pt2.RefreshTable
End With
End Sub
Last edited: