This question already has an answer here:
I'm running a simple shell command in Excel VBA that runs a batch file in a specified directory like below:
Dim strBatchName As String
strBatchName = "C:\folder\runbat.bat"
Shell strBatchName
Sometimes the batch file might take longer on some computer to run, and there are proceeding VBA code that is dependent on the batch file to finish running. I know you can set a wait timer like below:
Application.Wait Now + TimeSerial(0, 0, 5)
But that might not work on some computer that are too slow. Is there a way to systematically tell Excel to proceed with the rest of the VBA code until after the shell has finish running?
This is what I use to have
VB
wait for process to complete before continuing. I did not write this and do not take credit.It was offered in some other open forum and works very well for me:
The following declarations are needed for the
RunShell
subroutine:Either link the shell to an object, have the batch job terminate the shell object (exit) and have the VBA code continue once the shell object = Nothing?
Or have a look at this: Capture output value from a shell command in VBA?
Use the WScript.Shell instead, because it has a
waitOnReturn
option:(Idea copied from Wait for Shell to finish, then format cells - synchronously execute a command)
what you proposed with a change at the parenthesis at the Run command worked fine with VBA for me
Save the bat file on "C:\WINDOWS\system32" and use below code it is working
Dim wsh as new wshshell
chdir "Directory of Batch File"
wsh.run "Full Path of Batch File",vbnormalfocus, true
Done Son