如何阻止 Visual Basic 编辑器在宏运行后自动打开?

How to stop Visual Basic editor from automatically opening after macro runs?

我有工作代码,其中唯一的问题是它在 运行 时打开 Visual Basic 编辑器,关闭它,然后在完成后重新打开它。

我相信这是因为我正在创建工作簿,导入模块,并在关闭之前向每个工作簿添加一个 sheet 事件。这是 Visual Basic 编辑器弹出打开然后关闭的时间。

完成所有这些后,编辑器将再次打开并保持打开状态。

有没有什么方法可以阻止编辑器打开或在全部完成后关闭 window?

我最后的解决方案是在完成后关闭工作簿 运行。

ScreenUpdating 和 DisplayAlerts 在宏运行期间被禁用。

Sub MasterCopy()

Dim file_name As String
file_name = Dir(personal_path)
Dim wb_sel As Workbook

Do While file_name <> ""
    If Not IsFileOpen(personal_path & file_name) Then ''''function checking if file is open
        
        Set wb_sel = Workbooks.Add(master_path & master_name)
        
        Call add_code(wb_sel)
        
        wb_sel.SaveAs filename:=personal_path & file_name, FileFormat:=xlExcel12, ReadOnlyRecommended:=True
        wb_sel.Close (False)
    Else
        FailedUpdate.Add file_name '''this is a collection
    End If
    
    file_name = Dir()
Loop

End Sub

Sub add_code(ByVal wb_sel As Workbook)

Dim xLine As Integer
Dim xMod As VBIDE.CodeModule
Set xMod = wb_sel.VBProject.VBComponents("Sheet1").CodeModule
    
wb_sel.VBProject.VBComponents.Import (code_import) ''''imported module

With xMod
    xLine = .createeventproc("Change", "Worksheet")
    xLine = xLine + 1
    .insertlines xLine, "application.screenupdating = false" & vbLf & "Call rng_change(Target)" & vbLf & "application.screenupdating = true" ''''adding sheet event
End With

End Sub

Application.VBE.MainWindow.Visible = False

是解决方案。大功告成,全部完成后关闭 VBE window。