VBA & Macro Problems

Fix Word VBA Macro That Fails When Run Normally But Works Step-by-Step

Natalie TaylorNatalie Taylor Nov 3, 2026 868 views

Question details

The user's Word VBA macro executes successfully when stepping through the code line-by-line (using F8), but throws errors or fails to complete actions when executed normally at full speed (using F5).

How to Fix a Word VBA Macro That Works Step-by-Step but Fails When Run Normally
Product
Word
Device & OS
not provided
Scenario
Running a VBA macro that involves system input/output operations such as copying, pasting, or opening and saving documents.
Observed behavior
The macro code runs faster than the computer can process the I/O operations, causing the macro to fail during normal execution because it attempts to execute subsequent lines before previous system tasks have finished.
Before you start

Save a backup of your current Word document and copy your VBA code to a safe location before adding system loops or modifying timing functions.

Solution 1Recommended

Insert DoEvents to Allow System Operations to Complete

Using the built-in DoEvents function temporarily pauses macro execution, granting Word and Windows the necessary time to finish background I/O tasks.

When a macro runs at full speed, system commands like clipboard copying or file saving simply cannot keep up. Inserting the DoEvents command forces the VBA execution thread to yield, allowing the operating system to process queued events before the code proceeds to the next line.

1
Identify I/O Operations

Open your VBA editor and locate the exact lines in your code where heavy operations occur, such as copying data to the clipboard, pasting, or opening external files.

2
Insert the DoEvents Command

Type 'DoEvents' on a new line immediately following the I/O command. For particularly heavy operations, you can safely add it twice on consecutive lines to ensure the queue clears completely.

3
Test the Macro

Press F5 to run the macro normally. Verify that the timing issue is resolved and the macro processes all data without failing.

Insert DoEvents to Allow System Operations to Complete
Best Practice: DoEvents is generally the safest way to handle timing issues because it relies on the system's actual event queue rather than arbitrary time delays.
Free Microsoft Office alternative

Try WPS Office for a Smoother Macro Experience

If you frequently encounter frustrating timing bugs, execution errors, or heavy performance lags when running VBA macros in Microsoft Office, consider switching to WPS Office. WPS offers a highly compatible, built-in VBA editor that smoothly handles macros alongside a lightweight architecture designed to process I/O operations efficiently.

Fully compatible with Microsoft Word .doc, .docx, and macro-enabled formatsBuilt-in robust VBA environment for seamless script execution and debuggingLightweight application footprint that handles system memory and I/O tasks efficientlyFree to download and use with a highly familiar, easy-to-navigate user interface
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA code work when pressing F8 but not F5?

Pressing F8 executes your VBA code line-by-line, giving your computer ample time between steps to process background tasks like copying, pasting, or file saving. Pressing F5 runs the code at full speed, often outpacing the system's ability to complete these input/output operations, which results in execution errors.

Is Application.Wait or Sleep better for VBA timing issues?

The Sleep API is generally better for shorter, precise millisecond delays, though it pauses the entire thread. Application.Wait has a minimum delay of one second and is simpler to implement without API declarations. Often, combining a short Sleep with a DoEvents command yields the most reliable results.

What does the DoEvents function actually do in VBA?

DoEvents yields macro execution to the operating system temporarily. This allows Windows to process other pending events in its queue—such as keystrokes, UI updates, and most importantly, the file input/output or clipboard operations that your macro is waiting to finish before proceeding.

How do I declare the Sleep API in 64-bit Office versions?

For 64-bit versions of Office, you must include the 'PtrSafe' keyword in the API declaration to avoid compilation errors. Use this exact syntax at the top of your module: Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long).