VBA:在运行时通过userformbutton退出macros时重新启动macros导致错误429

我有Excel中的macros从其他两个工作簿中提取数据并导入它。 在此过程中,显示​​一个进度条,用户可以随时通过取消和标准Xbutton退出。

当我使用这两个button中的任何一个退出进程时,当我尝试再次启动macros时,它给了我错误429。 不知何故,我的表单仍然活跃,我猜。 在VBA编辑器中按下重置button后,我可以再次启动macros而不会出错。

我的表单上也有一个OKbutton,当导入过程完成后,它变为活动状态。

所有这三个button执行相同的代码片断,closures一切。 唯一的区别是它们在执行的不同点上使用,这使得debugging有点混乱。

我的进度条:

进度条

用户表单中的代码:

Dim Button As String Sub StartForm(CalledFrom As String) Button = CalledFrom End Sub Private Sub UserForm_Initialize() ProgressBar.PBOKButton.Enabled = False End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = 0 Then If ProgressBar.PBBar.Width < 200 Then If MsgBox("Wollen Sie den Vorgang wirklich abbrechen?", vbYesNo + vbQuestion, "Extrahiere Daten...") = vbNo Then 'Do nothing Cancel = True Else ExitProcess End If Else ExitProcess End If End If End Sub Private Sub UserForm_Activate() If Button = "GetDataButton" Then GetData End If End Sub Private Sub PBOKButton_Click() ExitProcess End Sub Private Sub PBCancelButton_Click() If MsgBox("Wollen Sie den Vorgang wirklich abbrechen?", vbYesNo + vbQuestion, "Extrahiere Daten...") = vbYes Then ExitProcess End If End Sub 

代码片段结束所有(存储在我的macros)

 Sub ExitProcess() KostenstellenWB.Close SaveChanges:=False MAStundenWB.Close SaveChanges:=False ExcelApp.Quit Set ExcelApp = Nothing End End Sub 

错误

错误

谢谢你的帮助。

我曾经在Word VBA中遇到过类似的问题,虽然我不记得,也没有find所有的细节,但是解决scheme是这样的:

 ' In module myUserForm ' Dim Button As String Dim myCancel as Boolean Sub StartForm(CalledFrom As String) Button = CalledFrom myCancel= False Me.Show ' Control returns after the form has been unloaded If (myCancel = True) Then ExitProcess End If End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = 0 Then If ProgressBar.PBBar.Width < 200 Then If MsgBox("Wollen Sie den Vorgang wirklich abbrechen?", vbYesNo + vbQuestion, "Extrahiere Daten...") = vbNo Then 'Do nothing Cancel = True Else myCancel = True Unload Me ' unload the form from memory End If Else myCancel = True Unload Me ' unload the form from memory End If End If End Sub 

也就是说,一旦表单被卸载,ExitProcess就会运行。 请注意,这仍然发生在用户表单模块中。 如果这不起作用,那么你必须将ExitProcess函数移动到StartForm的调用者,我认为这是在userform模块之外,所以你确定模块没有被使用了。

我通过将子例程ExitProcess移动到用户窗体代码中解决了这个问题,并删除了每个Unload Me 。 现在当用户通过取消button或右上angular的xbutton退出时, 只有例程ExitProcess被执行。

之后,我能够重新启动过程,没有任何错误。

 Sub ExitProcess() KostenstellenWB.Close SaveChanges:=False MAStundenWB.Close SaveChanges:=False ERWB.Close SaveChanges:=False ExcelApp.Quit Set ExcelApp = Nothing End End Sub Private Sub UserForm_Initialize() ProgressBarForm.PBOKButton.Enabled = False End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = 0 Then If ProgressBarForm.PBBar.Width < 200 Then If MsgBox("Wollen Sie den Vorgang wirklich abbrechen?", vbYesNo + vbQuestion, "Extrahiere Daten...") = vbNo Then 'Do nothing Cancel = True Else ExitProcess End If Else ExitProcess End If End If End Sub Private Sub UserForm_Activate() GetData End Sub Private Sub PBOKButton_Click() ExitProcess End Sub Private Sub PBCancelButton_Click() If MsgBox("Wollen Sie den Vorgang wirklich abbrechen?", vbYesNo + vbQuestion, "Extrahiere Daten...") = vbYes Then ExitProcess End If End Sub