繁体   English   中英

excel vba userform enableevents

[英]excel vba userform enableevents

我在 Office Excel 2013 中遇到 Excel VBA 用户窗体事件问题,如下所示

  1. 带有三个复选框 (CB1,2,3) 和两个按钮取消和确定的简单用户表单
  2. 检查 CB1 时设置 CB3 = false
  3. 检查 CB2 时设置 CB3 = false
  4. 检查 CB3 时设置 CB1 = false 和 CB2 = false

我已经阅读并理解了http://www.cpearson.com/excel/SuppressChangeInForms.htm关于抑制 UserForm 事件和部分它的工作......

在上面的列表中,上面的 2. 和 3. 在代码中正常工作(如下所示)并且 CB3 没有触发任何事件。 但是,当我执行 4. 检查 CB3 - 它会触发 CB1 和 CB2 的事件,即使我已将其设置为不触发事件。

感激地收到任何帮助,

此致

肖恩

代码:

Public EnableEvents As Boolean
Private Sub UserForm_Initialize()

    Me.EnableEvents = True

End Sub

Private Sub vboInputsSelected_Click()

    Me.EnableEvents = False
    vboPracticesSelected.value = False           'this line does NOT fire an event
    Me.EnableEvents = True

End Sub

Private Sub vboOutputsSelected_Click()

    Me.EnableEvents = False
    vboPracticesSelected.value = False           'this line does NOT fire an event
    Me.EnableEvents = True

End Sub

Private Sub vboPracticesSelected_Click()

    Me.EnableEvents = False
    vboInputsSelected.value = False           'this line DOES fire an event
    vboOutputsSelected.value = False          'this line DOES fire an event
    Me.EnableEvents = True

End Sub

这对我来说很好。 当事件进行时, If失败。 意识到EnableEvents变量本身不会采取任何措施来阻止事件。 它只是您创建的布尔值。 您需要在允许事件发生之前对其进行检查,以使其执行任何操作。

Public EnableEvents As Boolean

Private Sub vboInputsSelected_Click()
  If Not EnableEvents Then Exit Sub

  Me.EnableEvents = False
  vboPracticesSelected.Value = False
  Me.EnableEvents = True
End Sub

Private Sub vboOutputsSelected_Click()
  If Not EnableEvents Then Exit Sub

  Me.EnableEvents = False
  vboPracticesSelected.Value = False
  Me.EnableEvents = True
End Sub

Private Sub vboPracticesSelected_Click()
  If Not EnableEvents Then Exit Sub

  Me.EnableEvents = False
  vboInputsSelected.Value = False
  vboOutputsSelected.Value = False
  Me.EnableEvents = True
End Sub

根据这个参考

使用类的新实例是更好的做法

下面尝试调整代码:

'http://www.cpearson.com/excel/SuppressChangeInForms.htm
'https://riptutorial.com/vba/example/19036/best-practices
Private Type TView
    IsCancelled As Boolean
    EnableEvents As Boolean
End Type
Private this As TView
Public Property Get IsCancelled() As Boolean
    IsCancelled = this.IsCancelled
End Property
Public Property Get EnableEvents() As Boolean
    EnableEvents = this.EnableEvents
End Property

Private Sub UserForm_Initialize()
'...
this.EnableEvents = True
End Sub

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
On Error GoTo ExceptionHandling
this.EnableEvents = False

'some code that would cause an event to run

CleanUp:
    On Error Resume Next
    this.EnableEvents = True
    Exit Sub
ExceptionHandling:
    MsgBox "Error: " & Err.description & vbLf & Err.Number
    Resume CleanUp
    Resume 'for debugging
End Sub 

Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
'check example
If this.EnableEvents = False Then Cancel = True
'some code to run
End Sub 

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = VbQueryClose.vbFormControlMenu Then
        Cancel = True
        this.IsCancelled = True
        Me.Hide
    End If
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM