VBA & Macro Problems

How to Fix Excel VBA Application.OnTime Irregular Intervals

Kushani NimanthikaKushani Nimanthika Oct 16, 2026 870 views

Question details

The user needs a way to stabilize macro execution intervals that fluctuate erratically due to workbook and system activity delays.

How to Fix Excel VBA Application.OnTime Irregular Intervals
Product
Excel
Device & OS
not provided
Scenario
Scheduling a recurring VBA macro to run continuously at a highly precise and stable interval.
Observed behavior
The Application.OnTime method executes inconsistently, with unexpected delays or overlapping intervals ranging wildly (e.g., from 0.25 to 2.12 seconds) instead of maintaining a stable rhythm.
Before you start

Ensure your workbook is saved and create a backup. Implementing Windows API functions like SetTimer can cause the application to crash if the VBA code is interrupted before the timer is properly stopped.

Solution 1Recommended

Replace Application.OnTime with Windows API Timers

Use the Windows SetTimer and KillTimer APIs to bypass Excel's native scheduling limitations, achieving precise millisecond-level stability.

Excel's built-in Application.OnTime method only triggers when the application is idle. If a user interacts with the sheet or external processes run, the macro queue gets delayed. Utilizing Windows API timers delegates the countdown to the operating system, guaranteeing much stricter execution times.

1
Open the VBA Editor

Press Alt + F11 to launch the Visual Basic for Applications editor, then insert a new standard Module.

2
Declare the Windows API Functions

At the top of the module, declare the SetTimer and KillTimer functions according to your system architecture (using PtrSafe for 64-bit compatibility).

3
Create the StartTimer Subroutine

Write a subroutine named StartTimer that calls the SetTimer API, passing your desired interval in milliseconds (e.g., 1000 for one second).

4
Create the StopTimer Subroutine

Write a subroutine named StopTimer that utilizes the KillTimer API to terminate the timer safely.

5
Link Your Target Macro

Define the callback procedure that the SetTimer function will execute and place your macro payload inside it.

Replace Application.OnTime with Windows API Timers
Stable Timing Achieved: By testing with millisecond timestamps, you will observe that intervals remain highly consistent (around 0.99s to 1.01s) when using the Windows API approach.
Free Microsoft Office alternative

Try WPS Office for Smooth and Reliable Macro Execution

If you frequently encounter frustrating execution delays or software locking when running heavy VBA scripts in Microsoft Excel, consider making the switch to WPS Office. It provides a lightweight, highly compatible environment for running complex macros seamlessly.

  1. 1. Download WPS Office: Visit the official WPS Office website to download the free installation package.
  2. 2. Install the Software: Run the downloaded installer and follow the simple on-screen instructions to set up the suite on your computer.
  3. 3. Open Your Macro File: Launch WPS Spreadsheet and open your existing .xlsm files to execute and manage your VBA macros effortlessly.
Completely free to download and use with excellent performance.Highly compatible with Microsoft Excel macro-enabled formats (.xlsm, .xlsb).Built-in VBA editor and macro support out of the box.Lightweight architecture ensures smoother background execution and less UI blocking.
microsoft office alternative - wps office

Frequently Asked Questions

Why does Application.OnTime miss execution times in Excel?

Excel only processes the Application.OnTime queue when it is completely idle. Any user interaction, such as editing a cell, clicking the ribbon, or an open dialog box, will pause the application and delay the scheduled macro until the action is completed.

How can I accurately measure the execution interval of my VBA macro?

You can use the Windows API GetTickCount function within your VBA code to capture millisecond timestamps right at the beginning of your macro execution. Logging these stamps allows you to accurately measure the precise time gaps between runs.

Is it safe to use SetTimer and KillTimer in Excel VBA?

Yes, but it requires careful implementation. If the KillTimer function is not executed before the workbook is closed, or if the VBA code execution gets unexpectedly reset, the active timer will cause Excel to crash. Always ensure proper cleanup routines are tied to workbook closing events.