多个专用子工作表_在同一个工作表中更改

我有一个工作表中的以下子,但我需要在同一工作表中不同的单元格/枢轴另一个3。 我怎样才能做到这一点?

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'This line stops the worksheet updating on every change, it only updates when cell 'B1 or B2 is touched If Intersect(Target, Range("B1:B2")) Is Nothing Then Exit Sub 'Set the Variables to be used Dim pt As PivotTable Dim Field As PivotField Dim NewCat As String 'Here you amend to suit your data Set pt = Worksheets("Daily Overall").PivotTables("DailyOverallSignups") Set Field = pt.PivotFields("Reg Year") NewCat = Worksheets("Daily Overall").Range("B1").Value 'This updates and refreshes the PIVOT table With pt Field.ClearAllFilters Field.CurrentPage = NewCat pt.RefreshTable End With End Sub 

我认为,“相同”你的意思是,他们都需要在工作sheet_selectionchange? 由于你的代码当前退出,如果它不是b1:b2,通过添加其他范围来改变你的代码不会退出。 你也应该有error handling和enableevents在那里。

  Private Sub Worksheet_SelectionChange(ByVal target As Range) On Error GoTo Bummer 'This line stops the worksheet updating on every change, it only updates when cell 'B1 or B2 is touched If Not Intersect(target, Range("B1:B2")) Is Nothing Then 'if not nothing Application.EnableEvents = False 'Set the Variables to be used Dim pt As PivotTable Dim Field As PivotField Dim NewCat As String 'Here you amend to suit your data Set pt = Worksheets("Daily Overall").PivotTables("DailyOverallSignups") Set Field = pt.PivotFields("Reg Year") NewCat = Worksheets("Daily Overall").Range("B1").Value 'This updates and refreshes the PIVOT table With pt Field.ClearAllFilters Field.CurrentPage = NewCat pt.RefreshTable End With ElseIf Not Intersect(target, Range("c1:c2")) Is Nothing Then Application.EnableEvents = False MsgBox ("Foo") ElseIf Not Intersect(target, Range("d1:d2")) Is Nothing Then Application.EnableEvents = False MsgBox ("Bar") ElseIf Not Intersect(target, Range("e1:e2")) Is Nothing Then Application.EnableEvents = False MsgBox ("Hello World") Else Exit Sub End If MovingOn: Application.EnableEvents = True Exit Sub Bummer: MsgBox Err.Description Resume MovingOn End Sub