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
Run macro in background
提问by user1904295
I have some trouble with VBA macros.
我在使用 VBA 宏时遇到了一些麻烦。
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.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.
在打开
FILE1.xls
具有宏的 Excel 文件 ( ) 时(该宏包含一个带有命令按钮的用户窗体,我对其进行了编程以将这些命令按钮用作文件夹、网站等的快捷方式......)。这一步没有问题。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 ShowModal
property to False
in 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