Home » excel » excel – Run macro in background

excel – Run macro in background

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have some trouble with VBA macros.

  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.

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

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

How to&Answers:

You can set the UserForms ShowModal property to False in its properties window to prevent it blocking.

Answer:

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