Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run macro in background

Tags:

excel

vba

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.

like image 824
user1904295 Avatar asked Sep 14 '26 14:09

user1904295


2 Answers

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

like image 99
Alex K. Avatar answered Sep 16 '26 08:09

Alex K.


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
like image 24
Christoph Hauser Avatar answered Sep 16 '26 06:09

Christoph Hauser