VBA & Macro Problems

Excel VBA Shell: Wait for an Executable to Finish

John WilsonJohn Wilson Nov 27, 2026 868 views

Question details

Wait for an external executable launched by an Excel VBA Shell command and control its initial window style.

How to Wait for an Executable Launched by Excel VBA Shell
Product
Microsoft Excel VBA
Device & OS
Windows
Scenario
An Excel macro launches an external application and needs to continue after the application finishes.
Observed behavior
Shell starts the application but returns without waiting, and the application window does not reliably receive focus.
Before you start

Confirm the executable path and whether the application stays running until its work is complete. Open the VBA editor with Alt+F11 and locate the module containing the Shell command.

Solution 1Recommended

Wait for the Shell process with the Windows API

Use the process ID returned by Shell to pause the macro until the launched process exits.

Shell returns a process ID rather than waiting for completion. OpenProcess obtains a handle that WaitForSingleObject can use; CloseHandle releases it afterward. A missing VBA reference is not necessarily the cause.

This example waits indefinitely and blocks Excel's interface while the process runs. It waits for the launched process itself, not necessarily for any child processes it starts.

1
Add the API declarations

In the VBA editor, choose Insert > Module and add these declarations at the top of the module: #If VBA7 Then Private Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPtr Private Declare PtrSafe Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As LongPtr, ByVal dwMilliseconds As Long) As Long Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As LongPtr) As Long #Else Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long #End If

2
Launch and wait

Add the following procedure to the same module, replacing the path with your executable: Sub RunAndWait() Const SYNCHRONIZE As Long = &H100000 Const INFINITE As Long = -1 Const WAIT_OBJECT_0 As Long = 0 Dim processId As Long Dim waitResult As Long #If VBA7 Then Dim processHandle As LongPtr #Else Dim processHandle As Long #End If processId = CLng(Shell(Chr$(34) & "C:\ACI32\Applications\Report32.EXE" & Chr$(34), vbNormalFocus)) processHandle = OpenProcess(SYNCHRONIZE, 0, processId) If processHandle = 0 Then MsgBox "Could not open the process. It may have already exited, or access was denied." Exit Sub End If waitResult = WaitForSingleObject(processHandle, INFINITE) Call CloseHandle(processHandle) If waitResult <> WAIT_OBJECT_0 Then MsgBox "The process wait did not complete successfully." Exit Sub End If MsgBox "The launched process has exited." End Sub

3
Test the macro

Place the cursor inside RunAndWait and press F5. Close the launched application if it remains open; the completion message should appear after its process exits.

Wait for the Shell process with the Windows API
Window focus is not guaranteed: vbNormalFocus requests a normal, focused window, but Windows and the launched application ultimately control foreground activation. Waiting for process exit does not bring the window forward.
Free Microsoft Office alternative

Work with Excel files in WPS Spreadsheets

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Excel formats, making it easy to move everyday workbook work between suites. It does not fix Excel's VBA Shell behavior.

Open and edit Microsoft Excel XLSX and XLS filesUse a familiar spreadsheet interface for everyday workbook tasksMove existing spreadsheets to a lightweight office suiteWork on spreadsheet content separately from Windows process-launch troubleshooting
microsoft office alternative - wps office

Frequently Asked Questions

Does VBA Shell have a built-in wait option?

No. Shell returns after launching the program. On Windows, you can use its returned process ID with OpenProcess and WaitForSingleObject to wait for that process to exit.

Why doesn't vbNormalFocus always bring the application forward?

vbNormalFocus requests a normal, focused window, but Windows foreground rules and the launched application's behavior can prevent it from receiving focus.

What is the difference between /c and /k in ComSpec?

With Environ$("ComSpec"), /c runs the command and then closes the command processor, while /k leaves it open. Neither option makes the VBA Shell function wait by itself.

Why might the wait finish before the actual task is complete?

The launched executable may start another process and exit. WaitForSingleObject monitors the specific process handle you opened, not every child process or task it starts.

Do I need to add a VBA reference for the Windows API declarations?

No additional VBA reference is needed for the kernel32 declarations shown here. Place the declarations at the top of a standard module and use the conditional VBA7 declarations for compatible pointer sizes.