VBA to Open Another Instant of Excel and Run Macro

Ceeyee

Board Regular
Joined
Feb 2, 2011
Messages
164
Hi,

Is it possible in VBA to open another Excel file in another Excel instant (process) and more importantly, call a macro built in it?

The workbook.open method opens the file in the same Excel instant, which is not what we want.

Thanks.
 
Using the workbook_open event is good but it doesn't allow flexibility to run whatever macro you want in that moment. But if in the 2nd Excel instance you have created counterpart macros that use OnTime to call a specific macro after a 1 second delay then it will be the 2nd Excel instance that calls it, thereby allowing your 1st instance to carry on independently.
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Sorry cwild .. I forgot to post the vbs code

Try this in a standard module and run the Test routine :
Code:
Option Explicit

#If VBA7 Then
    Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
#Else
    Declare Function GetTickCount Lib "kernel32" () As Long
#End If


Sub RunRemoteMacroAsync(ByVal FilePathName As String, ByVal MacroName As String)
    Dim sVBSCode As String
    Dim iFileHandle As Integer
    Dim sTempVbs As String
    
    If Len(Dir(FilePathName)) <> 0 Then
        sTempVbs = Environ("Temp") & "\" & GetTickCount & "Temp.vbs"
        sVBSCode = "On Error Resume Next" & vbCrLf _
        & "With CreateObject(""Excel.Application"")" & vbCrLf _
        & ".Visible = True" & vbCrLf _
        & ".Workbooks.Open (""" & FilePathName & """)" & vbCrLf _
        & ".Application.Run """ & MacroName & """" & vbCrLf _
        & "End With" & vbCrLf _
        & "If Err<>0 Then" & vbCrLf _
        & " Msgbox ""Error Number:  "" & Err.Number & vbcrlf & ""Error Description : "" & Err.Description" & vbCrLf _
        & "End If" & vbCrLf _
        & "With CreateObject(""Scripting.FileSystemObject"")" & vbCrLf _
        & ".DeleteFile (Wscript.ScriptFullName)" & vbCrLf _
        & "End With"
        iFileHandle = FreeFile
        Open sTempVbs For Append As #iFileHandle
            Print #iFileHandle, sVBSCode
        Close #iFileHandle
        Call Shell("WScript.exe " & sTempVbs)
    End If
End Sub


Sub test()
    RunRemoteMacroAsync "C:\Test\MyFile1.xlsm", "MyMacro1"
    RunRemoteMacroAsync "C:\Test\MyFile2.xlsm", "MyMacro1"
    
[B][COLOR=#008000]    'rest of your caller application code goes here....[/COLOR][/B]
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,627
Messages
6,173,424
Members
452,515
Latest member
Alicedonald9

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top