在不断收到无限循环的情况下,如何解决此代码?

时间:2018-07-17 14:40:18

标签: excel-vba

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

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

1 个答案:

答案 0 :(得分:0)

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

此外,您还可以通过隐藏列来启动子项,因此无需在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

您可能需要为两个输入框都添加一些验证/错误处理。如果用户将工时数设为“ 1”会怎样?我建议调查Application.InputBox,以便您可以控制输入。