Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Loop Performance: How to Test It

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To test Excel VBA loop performance, wrap the loop in a timer using Timer or QueryPerformanceCounter, run it with a fixed, known workload, and compare the elapsed time across variants. The quickest reliable method is to record Timer before and after the loop, write the result to the Immediate window with Debug.Print, and run the same procedure several times to see the spread. This guide shows a repeatable test harness, how to avoid the measurement traps that make loops look faster or slower than they are, and what to change when the numbers do not make sense.

What You'll Need

  • Excel on Windows or Mac with the Visual Basic Editor available (Alt+F11 on Windows; on Mac, Tools > Macro > Visual Basic Editor, labels can differ slightly by version).
  • A workbook you can modify, ideally a copy, because the test writes values into cells.
  • Macro security set to allow macros in that workbook (File > Options > Trust Center > Trust Center Settings > Macro Settings on Windows; on Mac, Excel > Preferences > Security).
  • Basic familiarity with running a macro and opening the Immediate window (Ctrl+G in the editor).

Step-by-Step

  1. Create a clean test module. In the Visual Basic Editor, choose Insert > Module. Keep the test code separate from production code so you can delete it later without touching anything that matters.
  2. Define a fixed workload. Performance numbers are meaningless without a known amount of work. For example, write 10,000 rows of numbers into column A on a blank sheet, then have the loop read every cell in that range. Do not let the loop depend on whatever happens to be in the active sheet.
  3. Turn off the things that distort timing. At the start of the test procedure set Application.ScreenUpdating = False and Application.Calculation = xlCalculationManual. Restore both at the end, even if the code errors, using an error handler or a cleanup line. Screen repainting and automatic recalculation can dominate a short loop and hide the difference you are trying to measure.
  4. Add the timer. Declare Dim t As Double, then t = Timer immediately before the loop and Debug.Print "Elapsed: " & Format(Timer - t, "0.000") & " s" immediately after it. Timer returns seconds since midnight with limited resolution, so it is fine for loops that take a fraction of a second or more but poor for micro-benchmarks.
  5. For finer resolution, use the Windows API. In a standard module on Windows, declare QueryPerformanceFrequency and QueryPerformanceCounter with Declare PtrSafe statements, read the counter before and after, and divide the difference by the frequency. This gives sub-millisecond accuracy. This API is Windows-only; on Mac, use Timer or MicroTimer if your version provides it, and accept coarser results.
  6. Run the same test at least five times. The first run is often slower because of compilation and cold caches. Discard it and compare the remaining runs. Record each result in a worksheet column rather than only in the Immediate window, so you can see the minimum, maximum and typical value.
  7. Change one variable per comparison. Test the original loop, then a variant that reads the range into a Variant array first, then a variant that writes results in one block instead of cell by cell. Keep the workload, sheet and settings identical. If you change two things at once, you cannot tell which one helped.
  8. Log the environment alongside the result. Note the Excel version, whether the file is .xlsm, the sheet size, and whether other workbooks are open. A loop that takes 0.4 seconds in one session can take 2 seconds in another because of add-ins or a large calculation chain.
  9. Clean up. Restore ScreenUpdating and Calculation, clear the test range, and remove or comment out the timing lines before shipping the macro. Leaving Debug.Print inside a hot loop slows it down noticeably.

Common Mistakes

  • Timing the first run only. Compilation and cold memory make the first pass unrepresentative. Always discard it.
  • Leaving ScreenUpdating on. A loop that touches cells one at a time will spend most of its time repainting the screen, so the measurement reflects Excel's display, not your logic.
  • Measuring with the Immediate window full of output. Printing inside the loop adds overhead per iteration. Print once after the loop.
  • Comparing different workloads. A 5,000-row test and a 50,000-row test are not comparable. Fix the row count and the data type.
  • Ignoring calculation. If formulas recalculate on every cell write, the loop time includes Excel's calculation engine. Set calculation to manual for the test and restore it afterwards.
  • Assuming Timer is precise. Its resolution is coarse; a loop that reports 0.000 seconds may still be doing real work. Use the high-resolution counter for short loops.

Tips

  • Read a range into a Variant array in one statement, loop over the array in memory, then write the results back in one statement. This is usually the single biggest speed-up for cell-by-cell loops.
  • Use For Each over a Range only when you need cell objects; for pure number crunching, arrays are faster.
  • Keep the test procedure self-contained: it should create its own data, run the loop, log the time and clean up, so you can rerun it with one keypress.
  • Test on the same machine and the same workbook copy each time. Background processes and add-ins change results.
  • If you need to compare many variants, store results in a table with columns for variant name, run number and elapsed time, then chart it.

Troubleshooting

Results swing wildly between runs. Close other workbooks, disable add-ins temporarily, and run the test five times. If the spread remains large, the loop is probably interacting with the screen or with calculation; confirm both are off.

Elapsed time shows 0.000 seconds. The loop is shorter than Timer can resolve. Increase the workload (more rows) or switch to QueryPerformanceCounter on Windows.

The macro errors on the API declarations. On 64-bit Office, declarations must use PtrSafe and the correct pointer types. If you are on Mac, remove the API declarations entirely and use Timer.

ScreenUpdating stays off after a crash. In the Immediate window, type Application.ScreenUpdating = True and press Enter. Then add an error handler to the test procedure so cleanup always runs.

The optimized version is slower. Check that you are not copying the array back to the sheet inside the loop, and that the array is sized correctly. Also confirm the workload is identical; a larger range in the second test invalidates the comparison.

Frequently Asked Questions

Is Timer accurate enough for VBA loop tests?

It is fine for loops that take roughly a tenth of a second or longer. For shorter loops, increase the workload or use the high-resolution counter on Windows.

Why does my loop run faster the second time?

The first run includes compilation and cold caches. Later runs reuse compiled code and cached data, so always report a typical value from several runs, not the first.

Can I test performance on Mac the same way?

The Timer approach works on both platforms. The Windows API calls for high-resolution timing are not available on Mac, so use Timer there.

Does Application.ScreenUpdating = False change the result?

Yes, substantially, for loops that write to cells. Keep it off during the test and restore it afterwards so the workbook behaves normally.

Should I test with real data or generated data?

Generated data with a fixed size is better for comparisons because it removes variation. Test once with real data afterwards to confirm the result holds.