附加的VBA过程用于进度条userform。一切都按预期工作,除了取消按钮间歇无响应。
我间歇性地说,因为95%的时间我必须在程序停止前多次点击取消按钮。我可以看到按钮点击事件被动画,但程序没有被中断。在按钮按下事件发生之前,看起来有些东西正在从取消按钮中窃取焦点。
转义和窗口关闭按钮只需单击即可按预期响应。
要使取消按钮正确响应,我需要做什么?谢谢!
更新:我注意到当我点击并按住取消按钮时,而不是按下“按下”按钮,它会被重新启动。所以显然有些东西正在将按钮状态重置为up,足够快以至于程序没有捕获down状态来触发click事件。
以下是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
答案 0 :(得分:1)
每次第一次点击都适合我。尝试卸载任何可能在容器应用程序内运行的加载项或任何其他代码,看看是否有帮助。
答案 1 :(得分:1)
如果人们仍然需要答案,那么我研究并且破解了!
单击取消按钮,代码应为;
Private Sub CommandButton1_Click()
Unload Me
'this bit ends all macros
End
End Sub
答案 2 :(得分:0)
答案是对进度条使用模态用户表单,以便按钮点击事件可以触发而不会被调用过程中的处理遮挡。