用户表单取消button_Intermittently_没有响应?

附带的VBA程序适用于进度条用户窗体。 一切按预期工作,除了取消button是间歇性地没有响应。

我间歇地说,因为95%的时间我必须在程序停止之前多次点击取消button。 我可以看到button点击事件被animation,但程序没有被打断。 它看起来好像有什么东西在button向下事件发生之前从取消button上偷取焦点。

逃生和窗口closuresbutton按照预期作出响应,只需点击一下。

我需要做什么才能使取消button正确响应? 谢谢!

更新 :我注意到,当我点击并按住取消button,而不是停留在“按下”的button,它被踢了起来。 所以显然有些东西是重置button的状态,以至于程序没有捕捉到closures状态来触发点击事件。

以下是userform模块(名为UserForm1)中的代码:

Private mbooUserCancel As Boolean Public Property Get UserCancel() As Boolean UserCancel = mbooUserCancel End Property Private Property Let UserCancel(ByVal booUserCancel As Boolean) mbooUserCancel = booUserCancel End Property Public Sub UpdateProgress(CountTotal As Long, CountProgress As Long) On Error GoTo Error_Handler ProgressBar1.Value = CountProgress / CountTotal * 100 DoEvents Error_Handler: If Err.Number = 18 Then CommandButton1_Click End Sub Private Sub CommandButton1_Click() Hide UserCancel = True End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) Cancel = True CommandButton1_Click End Sub Private Sub UserForm_Activate() With Application .Interactive = False .EnableCancelKey = xlErrorHandler End With End Sub Private Sub UserForm_Terminate() Application.Interactive = True End Sub 

以下是调用UserForm1的模块(名为Module1)的代码:

 Sub TestProgress() On Error GoTo Error_Handler Dim objUserForm As New UserForm1 Dim lngCounter As Long Dim lngSubCounter As Long With objUserForm .Show vbModeless DoEvents For lngCounter = 1 To 5 If .UserCancel Then GoTo Exit_Sub For lngSubCounter = 1 To 100000000 Next lngSubCounter .UpdateProgress 5, lngCounter Next lngCounter Application.Wait Now + TimeValue("0:00:02") .Hide End With Exit_Sub: If objUserForm.UserCancel Then MsgBox "User Cancelled from UserForm1" End If Exit Sub Error_Handler: If Err.Number = 18 Then Unload objUserForm MsgBox "User Cancelled from Module1" End If End Sub 

每次在第一次点击时为我工作。 尝试卸载任何可能在容器应用程序中运行的加载项或其他代码,看看是否有帮助。

如果人们仍然需要这个答案,那么我研究和破解它!

点击取消button,代码应该是;

 Private Sub CommandButton1_Click() Unload Me 'this bit ends all macros End End Sub 

答案是使用模态用户表单进度条,以便button点击事件可以触发,而不会被调用过程中的处理所遮蔽。