当选择范围AR9:AR99时,如何更新此操作以执行不同的操作?

时间:2018-01-22 15:14:36

标签: excel-vba vba excel

如果目标是AR9:AR99,我想做另一个动作。我可以将其添加为else if语句吗?我已经尝试了几个没有按预期工作的选项。

 Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Application.ScreenUpdating = False

    If Intersect(Target, Range("G9:G99, W9:W99, AI9:AI99,AR9:AR99")) Is Nothing 
    Then Exit Sub
    ',V6:V25,AG6:AG25 milestone task to top 20 projects

    Dim pt As PivotTable
    Dim Field As PivotField
    Dim NewCat As String

    Set pt = Worksheets("mPIVOTS").PivotTables("PivotTable3")
    Set Field = pt.PivotFields("Project")

    NewCat = ActiveCell

    With pt
    Field.ClearAllFilters
    Field.CurrentPage = NewCat
    pt.RefreshTable
    End With

    Worksheets("Project - Milestone Dash").Activate

    Application.ScreenUpdating = True
    End Sub

1 个答案:

答案 0 :(得分:2)

如下所示。 (我还通过删除不必要的块来清理你的代码)

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Application.ScreenUpdating = False

    If Not Intersect(Target, Range("G9:G99, W9:W99, AI9:AI99,AR9:AR99")) Is Nothing Then

        ',V6:V25,AG6:AG25 milestone task to top 20 projects

        Dim pt As PivotTable
        Dim Field As PivotField
        Dim NewCat As String

        Set pt = Worksheets("mPIVOTS").PivotTables("PivotTable3")
        Set Field = pt.PivotFields("Project")

        NewCat = ActiveCell

        Field.ClearAllFilters
        Field.CurrentPage = NewCat
        pt.RefreshTable

        Worksheets("Project - Milestone Dash").Activate

    ElseIf Not Intersect(Target, Range("AR9:AR99")) Is Nothing Then

        'do different things

    End If

End Sub