我有一个脚本(见下文),它加载一个参考库,然后向用户呈现一个 GUI(用户窗体)。问题是,加载库后它会结束脚本。不是在命令之后立即(如崩溃),而是在它完成运行之后。在这种情况下,这意味着 GUI 不显示;然而,在重新运行脚本时(当它不必加载参考库,因为它已经加载时),GUI 显示正常。
我不想每次使用这个脚本都手动运行两次,另外我无法设置我的系统来记住这个参考库,有很多计算机运行这个脚本。
有没有办法:
在加载库时抑制脚本结束的这种行为
使脚本在完成一次运行后再次自动调用自己
Sub AddReference() 'Add a reference library to the project using the GUID Dim strGUID As String, theRef As Variant, i As Long On Error Resume Next strGUID = "{0002E157-0000-0000-C000-000000000046}" 'GUID for VBA Extensibility 5.3 'Remove any missing references For i = ThisWorkbook.VBProject.References.Count To 1 Step -1 Set theRef = ThisWorkbook.VBProject.References.Item(i) If theRef.isbroken = True Then ThisWorkbook.VBProject.References.Remove theRef End If Next i Err.Clear 'ThisWorkbook.VBProject.References.AddFromGuid GUID:=strGUID, Major:=1, Minor:=0 ThisWorkbook.VBProject.References.AddFromFile "C:\Program Files\Common Files\microsoft shared\VBA\VBA6\VBE6EXT.OLB" Select Case Err.Number Case Is = 32813 'Reference already in use Case Is = vbNullString 'Reference added without issue Case Else 'An unknown error was encountered, so alert the user MsgBox "A problem occurred while loading the reference library!" End End Select On Error GoTo 0 MyGUI.Show End Sub