VBa代码不会出现循环

问题看起来很大,但回答你们会很简单我有代码,第一次工作,而不是第二次尝试工作。 我有2张“菜单”和“子表格”基本上,我有数据validation下拉菜单中设置是/否值。 第一种情况select“是”将启用第二张(子图)上的单元格select“否”将禁用第二张(子图)上的单元格。

第二种情况,用户select“否”并select第二张将会提示他启用单元格“ok”并取消。 select“确定”将启用单元格和下拉列表中的值将被更改为“是”selectmsgprompt中的“取消”将禁用单元格,下拉列表中的值将保持为“否”不应显示消息提示,如果用户select了“是”在下拉列表中

问题:代码工作正常,直到第二种情况。 用户select“否”,并在消息提示中select第二张,他select“否”。 现在单元格被禁用。 如果用户返回菜单表并select“ 是”,将不会启用单元格。 不知道现在不在启用单元格。 请帮忙

菜单表上的代码

Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Range("A11")) Is Nothing Then Exit Sub Application.EnableEvents = False Select Case (Target.Value) Case "YES" Call uEnable Case "NO" Call uDisable Exit Sub End Select Application.EnableEvents = True End Sub 

代码在子表单上

 Private Sub Worksheet_Activate() UDisable End Sub 

代码模块

 Option Explicit Private mMessageDisplayed As Boolean Public Sub uDisable() If ActiveSheet.ProtectContents And Not mMessageDisplayed Then mMessageDisplayed = True If ThisWorkbook.Sheets("Menu").Range("A11") = "NO" Then If MsgBox("Cells are locked on current sheet, press ok to Unlock", vbOKCancel + vbInformation) = vbOK Then ThisWorkbook.Worksheets("Menu").Range("A11") = "YES" With ThisWorkbook.Sheets("Subsheet") ActiveWorkbook.Unprotect Password:="xyz" .Range("E13:E14").Locked = False ActiveWorkbook.Unprotect Password:="xyz" End With Else ThisWorkbook.Worksheets("Menu").Range("A11") = "NO" With ThisWorkbook.Sheets("Subsheet") ActiveWorkbook.Unprotect Password:="xyz" .Range("E13:E14").Locked = True ActiveWorkbook.Protect Password:="xyz" End With End If Else Exit Sub End If End If End Sub 

第二个模块

 Public Sub uEnable() With ThisWorkbook.Sheets("Subsheet") ActiveWorkbook.Unprotect Password:="xyz" .Range("E13:E14").Locked = False ActiveWorkbook.Protect Password:="xyz" End With End Sub 

我试图用debugging方法,找不到根本原因。

两个相交的代码

 `Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Range("E42")) Is Nothing Then Exit Sub Application.EnableEvents = False Dim inputCell As Range Set inputCell = Range("E43") Select Case (Target.Value) Case "Specific Days" inputCell.Locked = False inputCell.Activate Case Else 'This handles **ANY** other value in the dropdown inputCell.Locked = True ' inputCell.Clear End Select Application.EnableEvents = True If Intersect(Target, Range("E29")) Is Nothing Then Exit Sub Application.EnableEvents = False Select Case (Target.Value) Case "YES" Call Notify Case "NO" Call NotifyUserGeneral End Select Application.EnableEvents = True End Sub` 

Call uDisable下面删除Exit Sub 。 否则Application.EnableEvents = True永远不会被调用…

 Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub Application.EnableEvents = False Select Case (Target.Value) Case "YES" Call uEnable Case "NO" Call uDisable 'Exit Sub <---Can't do this. End Select Application.EnableEvents = True End Sub 

…并没有任何其他代码可以打开它们。 在closures事件处理之后,您不能依赖事件处理程序来设置Application.EnableEvents = True