vba 在后台运行宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13880920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:49:00  来源:igfitidea点击:

Run macro in background

excelvbaexcel-vba

提问by user1904295

I have some trouble with VBA macros.

我在使用 VBA 宏时遇到了一些麻烦。

  1. While opening a Excel file (FILE1.xls) which has a macro (the macro contains a userform with command buttons which i programmed it to use those command buttons as shortcuts to folders, website etc...). There is no problem with this step.

  2. I am trying to open a new Excel file (FILE2.xls) without macro (for example a file i received by mail) when I open the file it's invisible. I think it's a problem coming from my macro.

  1. 在打开FILE1.xls具有宏的 Excel 文件 ( ) 时(该宏包含一个带有命令按钮的用户窗体,我对其进行了编程以将这些命令按钮用作文件夹、网站等的快捷方式......)。这一步没有问题。

  2. FILE2.xls当我打开一个不可见的文件时,我试图打开一个没有宏的新 Excel 文件 ( )(例如我通过邮件收到的文件)。我认为这是来自我的宏的问题。

So I want to run this macro in background and be able to open 6 different Excel files (for example).

所以我想在后台运行这个宏并能够打开 6 个不同的 Excel 文件(例如)。

I have been searching to solve this problem for a long time.

我一直在寻找解决这个问题很长时间。

回答by Alex K.

You can set the UserForms ShowModalproperty to Falsein its properties window to prevent it blocking.

您可以在其属性窗口中将 UserFormsShowModal属性设置为False以防止其阻塞。

回答by Christoph Hauser

I have used the below function to open several workbooks in the background.

我使用下面的函数在后台打开了几个工作簿。

Example call:

示例调用:

Set newWorkbook = GetWorkbook(scoreCardLink)

Function:

功能:

Public Function GetWorkbook(fullFileName As String) As Workbook
Application.ScreenUpdating = False
Dim result As Workbook
Dim sFileName As String
On Error Resume Next

strFilePath = fullFileName
vParts = Split(strFilePath, "/")
sFileName = vParts(UBound(vParts))

Set result = Workbooks(sFileName)
If (result Is Nothing) Then
    Application.enableEvents = False
    Set result = Workbooks.Open(fullFileName, ReadOnly = True, IgnoreReadOnlyRecommend = False)
End If
Set GetWorkbook = result
End Function