1

我在 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关于用户窗体事件的抑制和部分它的工作原理......

在上面的列表中,上面的 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
4

2 回答 2

2

这对我很有效。事件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
于 2013-10-27T14:07:01.993 回答
0

根据这个参考

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

下面尝试调整代码:

'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

也可以看看

VBA 用户表单 – 人人指南,Paul Kelly

于 2021-11-24T09:33:45.790 回答