1

我想结合这两行代码,但不知道如何让它工作。它们都单独工作,但我希望第一行代码成为第一个操作,然后第二个 sub 成为第二个操作。每当工作表发生更改时,这些都应该执行。当“S”范围内的相应单元格与 A 或 B 列中更新的单元格在同一行中更新时,第一个例程应该只导致一个消息框。

第二个操作应该寻找范围“T7:T26”中的任何变化并提示一个消息框。

代码如下:

    Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range
Dim lRow As Long

If Target.CountLarge > 1 Then Exit Sub

On Error GoTo Whoa

Set myRng = Range("A7:B26")

Application.EnableEvents = False

If Not Intersect(Target, myRng) Is Nothing Then
    lRow = Target.Row

    If Range("S" & lRow).Value >= 16 Then sVar = _
    MsgBox("Will Enough Pre-Wave Resources be Available?", 4, "Attention!")

    If sVar = 7 Then Application.Undo
End If

    Letscontinue:
Application.EnableEvents = True
Exit Sub
    Whoa:
MsgBox Err.Description
Resume Letscontinue


    End Sub

    Private Sub Worksheet_Change(ByVal Target As Range)

Dim myRng As Range

    Set myRng = ThisWorkbook.Sheets("SMT 5").Range("T7:T26")

        For Each mycell In myRng

    If mycell.Value = "ISSUE" Then sVar = MsgBox("Possible Pre-Wave Manpower Issue on     2nd or 3rd Shift. Will Enough Resources be Available?", 4, "Attention!")

If sVar = 7 Then

Application.Undo

End If

Exit For

Next

End Sub
4

3 回答 3

0

如果它们都单独工作,您可以将代码复制到一个模块中并给它们两个不同的名称。然后,在 Worksheet_Change 子程序中,您只需使用它Call来运行两个子程序。

于 2013-10-16T20:47:01.473 回答
0
    Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range
Dim lRow As Long

If Target.CountLarge > 1 Then Exit Sub

On Error GoTo Whoa

Set myRng = Range("A7:B26")

Application.EnableEvents = False

If Not Intersect(Target, myRng) Is Nothing Then
    lRow = Target.Row

    If Range("S" & lRow).Value >= 16 Then sVar = _
    MsgBox("Will Enough Pre-Wave Resources be Available?", 4, "Attention!")

    If sVar = 7 Then Application.Undo
End If

Set othrRng = Range("T7:T26")

For Each aCell In othrRng

    If aCell.Value = "ISSUE" Then sVar = MsgBox("Possible Pre-Wave Manpower Issue on 2nd or 3rd Shift. Will Enough Resources be Available?", 4, "Attention!")

    If sVar = 7 Then

    Application.Undo
    Exit For

End If

Next

Letscontinue:
    Application.EnableEvents = True
    Exit Sub

Whoa:
    MsgBox Err.Description
    Resume Letscontinue

End Sub
于 2013-10-17T12:18:53.737 回答
0

这是你正在尝试的吗?

Const sMsg1 As String = "Will Enough Pre-Wave Resources be Available?"

Const sMsg2 As String = "Possible Pre-Wave Manpower Issue on " & _
"2nd or 3rd Shift. Will Enough Resources be Available?"

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim myRng As Range, othrRng As Range, aCell As Range
    Dim lRow As Long
    Dim sVar

    If Target.CountLarge > 1 Then Exit Sub

    On Error GoTo Whoa

    Set myRng = Range("A7:A26")
    Set othrRng = Range("T7:T26")

    Application.EnableEvents = False

    If Not Intersect(Target, myRng) Is Nothing Then
        lRow = Target.Row

        If Range("S" & lRow).Value >= 16 Then sVar = _
        MsgBox(sMsg1, 4, "Attention!")

        If sVar = 7 Then Application.Undo
    End If

    For Each aCell In othrRng
        If aCell.Value = "ISSUE" Then _
        sVar = MsgBox(sMsg2, 4, "Attention!")

        If sVar = 7 Then
            Application.Undo
            Exit For
        End If
    Next

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
于 2013-10-16T21:08:37.357 回答