excel - 如何修复此代码,我不断收到无限循环?
问题描述
当我运行此代码并在“批量”单元格中选择“是”时,我不断收到“请输入工时数”。基本上我的目标是有一个下拉列表来显示隐藏的行。然后,如果在另一个下拉列表中选择了“是”,则会出现两个额外的框输入
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
解决方案
更改单元格后,您可以立即禁用事件,然后在退出子程序之前重新启用它们。
此外,您可以通过隐藏列来启动 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
,以便您可以控制输入。
推荐阅读
- dataframe - 分解趋势、季节性和剩余时间序列元素的问题
- java - 如何在 for-each 循环中获取替代值?
- visual-studio-mac - Visual Studio Mac 错误!自定义命令执行失败
- javascript - Vanilla JS:如何返回动态 html 以便在 element.innerHTML 中重用它?
- java - 如何在 Android 应用中使用 TensorflowInferenceInterface
- pdo - “错误:找不到驱动程序”触发“致命错误:未捕获的错误:在 null 上调用成员函数 prepare()”
- node.js - Nodejs Bcrypt + Heroku 部署失败
- objective-c - Objective-C:登录页面后标签栏项目的标题不会改变,但从xCode重新启动时有效?
- android - 如何在支持 unicode 的 Android 中创建 PDF
- asp.net - 从另一个动作重定向到一个动作