首页 > 解决方案 > 如何修复此代码,我不断收到无限循环?

问题描述

当我运行此代码并在“批量”单元格中选择“是”时,我不断收到“请输入工时数”。基本上我的目标是有一个下拉列表来显示隐藏的行。然后,如果在另一个下拉列表中选择了“是”,则会出现两个额外的框输入

Private Sub worksheet_change(ByVal target As Range)
ActiveSheet.Activate
Rows("20:22").EntireRow.Hidden = True
Rows("23:26").EntireRow.Hidden = True
Rows("27:30").EntireRow.Hidden = True
Rows("51:56").EntireRow.Hidden = True
If Not Application.Intersect(Range("Change"), Range(target.Address)) Is Nothing Then
        Select Case target.Value
        Case Is = "Asset Transfer": Rows("20:22").EntireRow.Hidden = False
                            Rows("23:26").EntireRow.Hidden = True
                            Rows("27:30").EntireRow.Hidden = True
                            Rows("51:56").EntireRow.Hidden = True
        Case Is = "Fund Lineup": Rows("27:30").EntireRow.Hidden = False
                            Rows("20:22").EntireRow.Hidden = True
                            Rows("23:26").EntireRow.Hidden = True
                            Rows("51:56").EntireRow.Hidden = True
        Case Is = "Plan Merge": Rows("23:26").EntireRow.Hidden = False
                            Rows("20:22").EntireRow.Hidden = True
                            Rows("27:30").EntireRow.Hidden = True
                            Rows("51:56").EntireRow.Hidden = True
        Case Is = "Loans": Rows("51:56").EntireRow.Hidden = False
                            Rows("27:30").EntireRow.Hidden = True
                            Rows("20:22").EntireRow.Hidden = True
                            Rows("23:26").EntireRow.Hidden = True
                            Rows("28:31").EntireRow.Hidden = True
        End Select
End If
Set target = Range("bulk")
If target.Value = "Yes" Then
Dim QtyEntry As Integer
Dim Msg As String
Msg = "Please enter the number of labor hours'"
QtyEntry = InputBox(Msg)
ActiveSheet.Range("c60").Value = QtyEntry
Dim Entry As Integer
Dim Msg1 As String
Msg1 = "Enter percentage increase'"
Entry = InputBox(Msg1)
ActiveSheet.Range("d60").Value = Entry
End If
End Sub

标签: excelvba

解决方案


更改单元格后,您可以立即禁用事件,然后在退出子程序之前重新启用它们。

此外,您可以通过隐藏列来启动 sub,因此无需在Select Case. 您在这里需要做的就是取消隐藏您想要显示的行。

还有#2,你确定你不想在你的第一个 if 语句中使用你的第二个 if 语句吗?照原样,任何更改都会提示您的输入框。

您可以将您的代码简化为这样,这使您的逻辑更容易遵循。主要的收获是注意到在被禁用的事件之外没有做任何事情。

Option Explicit

Private Sub worksheet_change(ByVal target As Range)

Application.EnableEvents = False
    Union(Rows("20:30"), Rows("51:56")).EntireRow.Hidden = True
    If Not Application.Intersect(Range("Change"), Range(target.Address)) Is Nothing Then
        Select Case target.Value
            Case "Asset Transfer"
                Rows("20:22").EntireRow.Hidden = False
            Case "Fund Lineup"
                Rows("27:30").EntireRow.Hidden = False
            Case "Plan Merge"
                Rows("23:26").EntireRow.Hidden = False
            Case "Loans"
                Rows("51:56").EntireRow.Hidden = False
        End Select
    'Do you want your second IF statement here?
    End If

    If Range(“bulk”) = "Yes" Then
        Range("C60") = InputBox("Please enter the number of labor hours'")
        Range("D60") = InputBox("Enter Percentage Increase'")
    End If
Application.EnableEvents = True

End Sub

您可能需要为两个输入框添加一些验证/错误处理。如果用户在工时数上加上“一”会发生什么?我建议您进行调查Application.InputBox,以便您可以控制输入。


推荐阅读