单元格更改时运行代码。无法观看多个小区

时间:2017-10-29 17:00:39

标签: excel vba excel-vba

我想在工作表中查看2个单元格以查看用户是否更改了它们。如果他们这样做,我希望它根据哪个更改运行代码。我没有错误,但它忽略了其中一个单元格。这只能观看一个小区吗?

目前我正在使用“ElseIF”来检查其他单元格,我也尝试使用“,”将两者都放在if语句的第一部分中(崩溃了excel)

If Not Intersect(Target, Target.Worksheet.Range("A5, "E4")) Is Nothing Then

我尝试在If语句的第一部分使用“或”

If Not Intersect(Target, Target.Worksheet.Range("A5")) Is Nothing Or Not Intersect(Target, Target.Worksheet.Range("E4")) Is Nothing Then

(什么也没做)

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)


If Not Intersect(Target, Target.Worksheet.Range("A5")) Is Nothing Then

If [A5] = "Day Shift:" Then

    ActiveWorkbook.Unprotect "Welcome1"

    Worksheets("5001 Prog").Unprotect "Welcome1"

        Worksheets("5001 Prog").Range("25:120").EntireRow.Hidden = False

        Sheets("5001 HDD Days").Visible = True
        Sheets("5001 HDD Nights").Visible = False

        Worksheets("5001 Prog").Range("B5").Value = Worksheets("Variables").Range("E10").Value

        Worksheets("5001 Prog").Range("B4").Value = Worksheets("Variables").Range("M10").Value

        Worksheets("5001 Prog").Range("73:120").EntireRow.Hidden = True

    ActiveWorkbook.Protect "Welcome1"

Else

    ActiveWorkbook.Unprotect "Welcome1"

    Worksheets("5001 Prog").Unprotect "Welcome1"

        Worksheets("5001 Prog").Range("25:120").EntireRow.Hidden = False

        Sheets("5001 HDD Days").Visible = False
        Sheets("5001 HDD Nights").Visible = True

        Worksheets("5001 Prog").Range("B5").Value = Worksheets("Variables").Range("E11").Value

        Worksheets("5001 Prog").Range("B4").Value = Worksheets("Variables").Range("M11").Value

         Worksheets("5001 Prog").Range("25:72").EntireRow.Hidden = True

    Worksheets("5001 Prog").Protect "Welcome1"

    ActiveWorkbook.Protect "Welcome1"
End If

'Updates the last ream pass with the current foreman
    Call Update_Totals

'Selects the "5001 Prog" page
    Worksheets("5001 Prog").Select

ElseIf Intersect(Target, Target.Worksheet.Range("E4")) Is Nothing Then

'Updates the last ream pass with the current foreman
    Call Update_Totals

'Selects the "5001 Prog" page
    Worksheets("5001 Prog").Select

ElseIf Not Intersect(Target, Target.Worksheet.Range("E4")) Is Nothing Then

'Updates the last ream pass with the current foreman
    Call Update_Totals

'Selects the "5001 Prog" page
    Worksheets("5001 Prog").Select


Else
End If

End Sub

0 个答案:

没有答案