结合两个潜艇变化的范围

我想结合这两行代码,但不知道如何让它工作。 他们都分开工作,但我希望第一行代码是第一个操作,然后第二个子是第二个操作。 这些应该在工作表发生变化时执行。 当“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 

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

这是你正在尝试?

 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 
  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