Merging two worksheet_change (ByVal Target as Range) events into same Worksheet

TrueBlue2

New Member
Joined
Feb 18, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I'm new to VBA ... thanks for your patience and assistance.
I'm trying to create a report from multiple pivot tables on a single worksheet with each pivot table filter referencing a separate cell value in the worksheet.
I have VBA for each Worksheet_Change but I haven't been able to figure out how to merge them into a single Worksheet_Change as only one can exist within the worksheet.
Here are the three individual Worksheet_Change events (all work individually):

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next
If Intersect(Target, Range("A1:A1")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("NEW-District Report").PivotTables("PivotTable1")
Set xPFile = xPTable.PivotFields("District Name")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next
If Intersect(Target, Range("D1:D1")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("NEW-District Report").PivotTables("PivotTable2")
Set xPFile = xPTable.PivotFields("District Name")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next
If Intersect(Target, Range("G1:G1")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("NEW-District Report").PivotTables("PivotTable3")
Set xPFile = xPTable.PivotFields("District Name")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xPTable As PivotTable, xPFile As PivotField, xStr As String
    On Error Resume Next
    If Intersect(Target, Range("A1,D1,G1")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Select Case Target.Column
        Case Is = 1
            Set xPTable = Worksheets("NEW-District Report").PivotTables("PivotTable1")
            Set xPFile = xPTable.PivotFields("District Name")
            xStr = Target.Text
            xPFile.ClearAllFilters
            xPFile.CurrentPage = xStr
        Case Is = 4
            Set xPTable = Worksheets("NEW-District Report").PivotTables("PivotTable2")
            Set xPFile = xPTable.PivotFields("District Name")
            xStr = Target.Text
            xPFile.ClearAllFilters
            xPFile.CurrentPage = xStr
        Case Is = 7
            Set xPTable = Worksheets("NEW-District Report").PivotTables("PivotTable3")
            Set xPFile = xPTable.PivotFields("District Name")
            xStr = Target.Text
            xPFile.ClearAllFilters
            xPFile.CurrentPage = xStr
    End Select
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Mumps ... that worked perfectly ... I never would have been able to put that together with my limited knowledge. Thank you!!!!
 
Upvote 0
You are very welcome. :)
 
Upvote 0

Forum statistics

Threads
1,214,879
Messages
6,122,065
Members
449,064
Latest member
scottdog129

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