VBa代码不会出现循环

时间:2016-11-02 15:46:28

标签: excel vba excel-vba

问题看起来很大,但你们的答案很简单 我有第一次使用的代码,而不是第二次尝试的代码。 我有2张"菜单"和"子表" 基本上,我在菜单表上设置了是/否值的数据验证下拉菜单。 第一种情况 选择"是"将启用第二张纸上的单元格(子表格) 选择"否"将禁用第二张纸上的单元格(子表格)。

第二种情况 用户选择"否"并选择第二张表将提示他启用单元格" ok"并取消。 选择"确定"将启用单元格和下拉列表中的值将更改为" yes" 选择"取消"在msgprompt中将禁用单元格,并且下拉列表中的值将保持为" no" 如果用户选择了"是"则不应显示消息提示。在下拉..

问题:代码工作正常,直到第二种情况。 用户选择"否"并在消息提示中选择第二张,他选择" no"。现在细胞被禁用了。 如果用户返回菜单表并选择" 是",则不会启用单元格。 不知道现在不能启用细胞是什么。请帮忙

菜单表上的代码

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

SubSheet上的代码

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

我尝试使用调试方法,无法确定根本原因。

两个交叉码

`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`

1 个答案:

答案 0 :(得分:4)

Exit Sub下方移除Call uDisable。否则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

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