在("Menu")
范围内,E15:E25
我输入的值通过一些计算反映在工作表("Pack Plan")
范围中B5:P5
,然后在("Pack Plan")
范围内B6:P6
我有其他计算值。我需要一个代码来撤消("Menu")
范围E15:E25
和 MsgBox“调整包装计划”中的任何更改,如果该更改导致("Pack Plan")
范围内任何单元格B5:P5
的值小于下方同一列 1 行中单元格的值,(offset(-1, 0))。目前我有 15 个 IF 来执行此操作。我需要一个 IF 参数,它允许添加更多条件,而不必为每个 IF 复制它们。("Menu")
是活动表。
类似问题的答案对我不起作用。谢谢你。
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If (Target.Column = 5) Then
'I'm trying to consolidate the following IF arguments into 2 set's of IF code instead of having IF's for each cell in range.
'First these two If's:
If Worksheets("Crème").Range("C11").Value > Worksheets("Pack Plan").Range("B5").Value Then
MsgBox "Missing Ingredient!"
Application.Undo
End If
If Worksheets("Crème").Range("C12").Value > Worksheets("Pack Plan").Range("I5").Value Then
MsgBox "Missing Ingredient!"
Application.Undo
End If
'Then the following 15 IF's:
If Worksheets("Pack Plan").Range("B5").Value < Worksheets("Pack Plan").Range("B6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("C5").Value < Worksheets("Pack Plan").Range("C6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("D5").Value < Worksheets("Pack Plan").Range("D6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("E5").Value < Worksheets("Pack Plan").Range("E6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("F5").Value < Worksheets("Pack Plan").Range("F6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("G5").Value < Worksheets("Pack Plan").Range("G6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("H5").Value < Worksheets("Pack Plan").Range("H6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("I5").Value < Worksheets("Pack Plan").Range("I6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("J5").Value < Worksheets("Pack Plan").Range("J6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("K5").Value < Worksheets("Pack Plan").Range("K6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("L5").Value < Worksheets("Pack Plan").Range("L6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("M5").Value < Worksheets("Pack Plan").Range("M6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("N5").Value < Worksheets("Pack Plan").Range("N6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("O5").Value < Worksheets("Pack Plan").Range("O6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
If Worksheets("Pack Plan").Range("P5").Value < Worksheets("Pack Plan").Range("P6").Value Then
MsgBox "Adjust Packout Plan"
Application.Undo
End If
End If
' code to be consolidated ends here
If (Target.Column = 3) Then
If (Target.Offset(0, 2)) <> "" Then
Application.Undo
MsgBox "Clear Batch Size First", vbExclamation, "RESTRICTED"
End If
End If
Application.EnableEvents = True
End Sub
根据您对问题的新的和改进的描述,我修改了我建议的解决方案。请尝试下面的代码。它应该运行以响应“Menue”选项卡上的 Change 事件。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WsPack As Worksheet
Dim C As Long ' loop counter: Column
Select Case Target.Column
Case 5
Set WsPack = Worksheets("Pack Plan")
With Worksheets("Crème")
If .Cells(11, "C").Value > WsPack.Range("B5").Value Or _
.Cells(12, "C").Value > WsPack.Range("I5").Value Then
AppUndo 2
Exit Sub ' I added this
End If
For C = 2 To 16
If .Cells(5, C).Value > WsPack.Cells(6, C).Value Then
AppUndo 3
Exit Sub ' I added this
End If
Next C
End With
Case 3
If (Target.Offset(0, 2)) <> "" Then AppUndo 1
End Select
End Sub
Private Sub AppUndo(Idx As Integer)
Dim Msg(1 To 3) As Variant ' Message Texts, title, style
Msg(1) = Array("Clear batch size first", "RESTRICTED", vbExclamation)
Msg(2) = Array("Missing ingredient", "INCOMPLETE PREPARATION", vbCritical)
Msg(3) = Array("Adjust packout plan", "EXCESS CONTENT", vbExclamation)
MsgBox Msg(Idx)(0), Msg(Idx)(2), Msg(Idx)(1)
With Application
.EnableEvents = False
.Undo
.EnableEvents = True
End With
End Sub
如您所见,我并没有真正减少测试次数,而是通过使用 (a) 循环和 (b) 子例程来提高语法的效率,这两者都用于减少和避免重复代码。
讲解员(2020 年 8 月 26 日)
请注意这部分代码背后的基本原理。
Set WsPack = Worksheets("Pack Plan")
With Worksheets("Crème")
If .Cells(11, "C").Value > WsPack.Range("B5").Value Or _
.Cells(12, "C").Value > WsPack.Range("I5").Value Then
有两个工作表可供参考。其中一个在With
语句中声明,另一个作为变量。它们都不是(必然)ActiveSheet。相反,Worksheets("Crème") 中的单元格With
通过前导句点链接到语句,这对于不引用 ActiveSheet 而是引用已命名的表至关重要。必须标识另一个工作表中的单元格,并给出变量的全名。因此,选择一个短名称是有利的。"Worksheets("Pack Plan")" 也可以工作,但它并不短。
哪个工作表是由变量声明的,哪一个是由With
语句声明的,这取决于每个工作表被引用的次数。显然,该With
语句使代码更短。这就是为什么这是适用于大多数参考文献的方式。当我编写代码时,我认为两者都被同样频繁地引用,因此我选择了行中的第一张表来链接到With
语句以提高可读性。但是现在您说我在确定要引用的工作表时弄错了,并且大部分引用WsPack
. 因此,您可能更愿意颠倒使用变量和With
语句以适应修改后的用法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句