如果另一个工作表中的范围小于下面的单元格,则撤消 ActiveSheet 中的更改

("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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果工作表的工作表的ActiveSheet中的单元格值发生更改,则清除内容

PHPexcel从另一个工作表中获取数据并将其显示在activeSheet上

如果单元格范围等于另一个工作表中的单元格范围,则添加注释

使用间接访问另一个工作表中的单元格范围

VBA基于另一个工作表中的单元格更改列

根据另一个工作表中的值更改单元格的颜色?

ActiveSheet.PasteSpecial中的VBA PasteSpecial错误

Excel Chart范围基于另一个工作表中单元格中的值

从范围复制单元格值并将其粘贴到另一个工作表的单行中

选择范围内的单元格将值输入到另一个工作表中的单元格中

复制一个工作表中的单元格范围,并将其作为值而不是公式粘贴到另一个工作表中

如果出现在另一个带有宏的工作表中,则突出显示单元格

使用数据验证更改一个单元格后,如何自动更新单元格以匹配另一个工作表中的值?

在另一个工作簿中引用单元格作为工作表的名称

在Excel中引用另一个工作表中的单元格

当另一个工作表中的特定单元格值更改时触发脚本

根据其值引用另一个工作表中的另一个单元格

Excel VBA:动态更改工作表中其名称在另一个单元格中引用的单元格值

复制等于另一个工作表中单元格值的单元格

如果另一个单元格更改值,则在单元格中插入函数

引用双引号(“”)内另一个工作表中的单元格

引用另一个工作表中的单元格

在另一个工作表中粘贴单元格的位置 - Excel VBA

在另一个工作表的上方单元格中添加1

复制特定单元格并粘贴到另一个工作表中

Excel:在另一个工作表中的引用单元格上使用公式

将单元格粘贴到VBA中的另一个工作表

引用另一个工作表中的单元格

vba 输入公式引用另一个动态工作表中的单元格