VBA & Macro Problems

Fix an Excel VBA Macro That Stops After a Fixed Number of Lines

Huda QurayshiHuda Qurayshi Nov 14, 2026 868 views

Question details

Identify why an Excel or Access VBA procedure stops unexpectedly during an automated run.

Fix an Excel VBA Macro That Stops After a Fixed Number of Lines
Product
Microsoft Excel or Microsoft Access VBA
Device & OS
Windows
Scenario
A VBA procedure containing DoEvents is launched manually or through Windows Task Scheduler.
Observed behavior
The procedure appears to stop after a fixed number of lines or hangs unpredictably during a scheduled run.
Before you start

Open the VBA procedure and identify its DoEvents calls. Check whether the issue occurs when you run the procedure manually, through Task Scheduler, or both.

Solution 1Recommended

Test the VBA procedure without DoEvents

Use this test to determine whether yielding to the operating system contributes to the interruption.

DoEvents yields control so pending events can be processed. That can allow unexpected event handling or re-entry while a procedure is running; stopping at a particular line does not by itself indicate a fixed VBA line limit.

1
Find DoEvents calls

In Excel or Access, press Alt+F11 to open the VBA editor. Open the affected module and use Edit > Find to search for DoEvents.

2
Run a comparison test

Temporarily comment out a DoEvents call by placing an apostrophe at the start of its line. Run the procedure with F5, then compare the result with a run that includes the call.

3
Test a controlled delay

If the procedure needs a brief pause but does not need to process events during it, replace the call with a tested Windows Sleep delay. For VBA7 on Windows, add this declaration at the top of a standard module: Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long). Then use Sleep 100 to pause for 100 milliseconds.

4
Repeat the scheduled run

Save the changes and start the task from Task Scheduler using Run. Check whether the procedure completes under the same conditions as the failed run.

Test the VBA procedure without DoEvents
Consider responsiveness: Sleep blocks the VBA thread for the specified interval, so the application may become unresponsive during the pause. Removing DoEvents can also change behavior if the procedure relies on event processing.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface and support for common Microsoft Office formats, making it easy to migrate everyday spreadsheet work. It does not fix Excel's VBA or Task Scheduler behavior.

Open and edit Excel-compatible XLSX spreadsheetsUse a familiar spreadsheet interface for everyday workbook tasksWork with Word and PowerPoint-compatible document formatsMove routine Office work to a lightweight suite
microsoft office alternative - wps office

Frequently Asked Questions

Does VBA have a fixed limit on the number of lines a macro can run?

A macro stopping at a particular line does not necessarily mean it has reached a fixed execution limit. In the VBA editor, inspect that line and test nearby DoEvents calls to narrow down the cause.

What does DoEvents do in a VBA procedure?

DoEvents yields control so the operating system can process pending events. This can keep an application responsive, but it can also allow event handlers or the procedure itself to run unexpectedly while the original call is still active.

How can I test whether Task Scheduler's session is causing the problem?

Open the task's Properties in Task Scheduler. On the General tab, select Run only when user is logged on, sign in as the selected user, and run the task again.

Can I replace every DoEvents call with Sleep?

Not automatically. Sleep pauses the VBA thread without serving the same event-processing purpose. Test each replacement, especially if the procedure depends on user interaction, event handlers, or application responsiveness.

Will WPS Office fix a scheduled Excel VBA macro?

No. WPS Office is an alternative for everyday Office files; it does not repair Excel VBA or Windows Task Scheduler. Check macro compatibility separately before moving a VBA-dependent workbook.