VBA - IF不相交仅工作一次

时间:2016-04-02 19:04:40

标签: excel vba

每次在特定范围的单元格中输入日期时,我都会尝试触发排序。我第一次打开工作簿时,下面的代码正在运行但只有一次。如果我执行另一个操作或输入多个日期,它不会重复?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
    .DisplayAlerts = False
End With

    If Not Intersect(Target, Range("H4:H2500")) Is Nothing Then

        Rows("4:2500").Select
    ActiveWorkbook.Worksheets("Detail_Plan").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Detail_Plan").Sort.SortFields.Add Key:=Range( _
        "H4:H2500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Detail_Plan").Sort
        .SetRange Range("A4:AU2500")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    End If

With Application
    .ScreenUpdating = True
    .EnableEvents = False
    .Calculation = xlCalculationAutomatic
    .DisplayAlerts = True
End With

End Sub

1 个答案:

答案 0 :(得分:1)

您完成后不会重新启用活动,选择更改是一个事件

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
    .DisplayAlerts = True
End With