How to make VBA save, close and re-open a workbook as a new separate Excel instance?

Sam Hamels

New Member
Joined
Mar 20, 2018
Messages
49
Hi All,


I use the following code (found here) to do the following:

- Save
- Set up a Windows prompt that will re-open the Excel file in a few seconds
- Close the workbook
- Prompt triggers a re-opening of the workbook

Code:
Sub SaveCloseReOpen()
Dim strCMD As String


strCMD = "CMD /C PING 10.0.0.0 -n 1 -w 5000 >NUL & Excel.exe " & Chr(34) & ThisWorkbook.FullName & Chr(34)


ThisWorkbook.Save
Shell strCMD, vbNormalFocus
If Application.Workbooks.Count = 1 Then
    Application.Quit
Else
    ThisWorkbook.Close SaveChanges:=False
End If


End Sub


I need to run several separate instances of Excel at the same time, and they each need to use this code.
The separate Excel instances are originally set-up by left-mouse clicking the open Excel program in the bottom toolbar and clicking Excel while continuously holding ALT (you then get a prompt asking if you want to open a new separate Excel instance). This allows me to run macro's in each separate Excel instance simultaneously.

The problem is that, when each of the separate Excel instances executes the code above, they will all be re-opened in a single Excel instance.
My question therefore is: how could I adapt the code above, to make the Windows prompt re-open the Excel file as a new, separate Excel instance. :confused:

After being re-opened, each Excel instance (workbook) automatically starts executing a macro, which doesn't work out when they re-open all together as one (non-separated) Excel instance, because the whole instance then becomes 'busy' doing one single macro that started running in whichever instance/workbook that re-opened first.



Cheers,
Sam
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
.
Try naming the specific workbook/s instead of using "ThisWorkbook".

Google saving and closing workbook by name
 
Upvote 0
.
Try naming the specific workbook/s instead of using "ThisWorkbook".

Google saving and closing workbook by name

Each Excel file (workbook) that should be opened and running macro's in computationally separate Excel instances is already given a unique name.

Having the above code running in all workbooks already works in the sense that each unique workbook is saved, closed and re-opened by its own Windows prompt.

The problem is that, after re-opening, I now have one instance of Excel open (in task manager), with each workbook re-opened as part of the same Excel instance.

This means that, as soon as a macro starts running in one of the workbooks, all other workbooks freeze up. In that moment, all open workbooks are part of the same 'single' program 'Excel' that is running.

If, of the other hand, I would succeed in each workbook re-opening as a computationally separate instance of Excel (i.e. with its own separate spot in task manager), then each workbook can run its own macro's simultaneously.
 
Last edited:
Upvote 0
Try passing the /x switch to the commandline :

strCMD = "CMD /C PING 10.0.0.0 -n 1 -w 5000 >NUL & Excel.exe " & Chr(34) & ThisWorkbook.FullName & Chr(34) & "/x" & Chr(34)
 
Last edited:
Upvote 0
I have no idea WHY that works, but it WORKS!

Thank you very much :biggrin:

Passing the /x switch in the commandline string is the same as executing excel.exe /x from the Run command to open a new instance of excel.

Glad it worked for you.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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