The Shell
command you're using is asynchronous meaning it can run more than one process at a time, so in the case of your two Shell
statements in a row, both will be executed simultaneously.
An alternative way is to instead use the Run
command of Windows Script Host (WScript.Shell
) since it has more options including one to wait for execution of the program to finish before it continues.
Sub ShellWait(fName As String, Optional showWindow As Boolean = True)
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
wsh.Run fName, -showWindow, True
End Sub
You can also hide the window completely by specifying False
as the second parameter. (Caution with that option if user input is required!)
For example:
Sub demo()
ShellWait "x:\test.bat"
Beep
MsgBox "Finished running!"
End Sub
More Information: