将两个潜艇组合在一起,改变范围

时间:2013-10-16 20:15:09

标签: excel vba excel-vba excel-2007

我想结合这两行代码,但无法弄清楚如何让它工作。它们都是分开工作,但我希望第一行代码是第一个操作,然后第二个子代码是第二个操作。只要工作表发生更改,就应执行这些操作。第一个例程只应在" S"中的相应单元格时产生一个msg框。范围更新与A列或B列中更新的单元格位于同一行。

第二个操作应该寻找范围的任何变化" T7:T26"并提示一个msg框。

代码如下:

    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

3 个答案:

答案 0 :(得分:0)

如果它们都是自己工作的,您可以将代码复制到模块中,并为它们提供两个不同的名称。 然后,在Worksheet_Change子项中,您只需使用Call来运行两个子。

答案 1 :(得分: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

答案 2 :(得分: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