Skip to content
Daniele Penza edited this page Dec 5, 2026 · 5 revisions

⚑ VBA Performance β€” measured recommendations

Evidence Excel Runs Method Cross-check Release

Advice about VBA speed is easy to find and hard to trust: much of it was written for Visual Basic 6 on 32-bit Windows. These pages keep what was timed on current Excel, explain the traps that change results while optimizing, and list the rules of the vba-performance skill with their measurement status: πŸ“ measured, or πŸ§ͺ still to be measured.

πŸš€ Start here

I want to Go to
Install the skill and run the checker Quick start in the README
Understand a checker finding Skill Rules
Speed up a slow macro by hand The cost table below, then the page it names
Optimize without changing results Correctness Traps
Repeat or extend a measurement Measurement Method

🧭 How to read the pages

Glyph Meaning
πŸ”΄ Costs seconds or more over 100,000 calls or loop passes: fix first
🟠 Costs tenths of a second over 100,000: worth fixing in hot loops
🟒 Nanoseconds: change only for clarity
βœ… / ❌ Recommended form / form to avoid
⚠️ Correctness trap: the faster form can change results or Excel's state
πŸ“ Measured on Excel 365 64-bit
πŸ§ͺ Not yet measured: treat the figure as an estimate

Figures are medians on one Windows desktop (run K also on a second one). Rely on ratios more than on absolute times, and quote two significant figures. See Measurement Method.

πŸ† The costs that matter most

Pattern Measured cost Fix Page
πŸ”΄ Writing cells while the workbook holds volatile cells 5-5.6 ms per write for 500 volatile UDF cells (103x non-volatile) Drop Application.Volatile; calculation manual, one Calculate Working with Excel
πŸ”΄ Large array passed ByVal ... As Variant 3.4 ms per call (26,000x ByRef) ByRef Passing Data
πŸ”΄ Debug.Print on every pass 0.45-1.8 ms per line with the editor open (10,700-24,000x) Behind a Const debug flag, or every 1,000 passes Working with Excel
πŸ”΄ Linear search once per row 650 us per search in 100,000 values (1,100x binary search) Binary search or Dictionary Searching, Sorting and Collections
πŸ”΄ col(i) in For i = 1 To col.Count 27 us per read at 20,000 items (520x For Each) For Each Searching, Sorting and Collections
πŸ”΄ Nested loops comparing every pair 0.12 s at 5,000 items, quadratic (19x a Dictionary) One Dictionary pass Searching, Sorting and Collections
πŸ”΄ DoEvents on every pass 46 us per call (1,700x) Every 1,000 passes Working with Excel
πŸ”΄ S = S & X in a loop 11x a buffer at 10,000 pieces, 35x at 30,000 Join or a Mid$ buffer Strings
πŸ”΄ ADODB.Stream ReadText of a large UTF-8 file 11.3 s for 10 MB (700x MultiByteToWideChar) Get + one API call Files and CSV
πŸ”΄ Select + ActiveCell with ScreenUpdating on 1.6 ms per cell (55x a direct write) Write through the range; screen off Working with Excel
πŸ”΄ Deleting rows in a loop 0.86 ms per row (152x an array rebuild; a Union was 3.5x slower still) Rebuild the kept rows in an array (a block of values), or sort and delete one block Working with Excel
πŸ”΄ Writing cells that formulas depend on, no guard 0.62 ms per write (17.5x with the three guards off, 350x one block write) Calculation manual first, then screen and events Working with Excel
πŸ”΄ Range.Find per lookup 0.57 ms per call at 1,000 rows, 58 ms at 100,000 (46-106x VLookup) Match(k, keys, 1) on sorted keys, binary search, or a Dictionary Working with Excel
πŸ”΄ CreateObject("Scripting.Dictionary") on every pass 0.4 ms per creation (130-220x one reused) Create it once, RemoveAll on each pass Calls, Classes and Binding
πŸ”΄ UsedRange in a loop condition 0.21 ms per pass (7,200-10,600x a local copy) Read the last row once Working with Excel
πŸ”΄ Formatting cells one at a time 184 us per cell (595x one call on the block) Format the block once Working with Excel
πŸ”΄ The status bar set on every pass 62-72 us per update (980-1,500x) Every 1,000 passes Working with Excel
πŸ”΄ Reading and writing cells one at a time 31 us per row (42-46x one block, at any size) One .Value2 read and one write Working with Excel
πŸ”΄ Evaluate or [A1] per row 30 us per call (260-290x VBA) Read once; one Evaluate over the block Working with Excel
πŸ”΄ Exact VLookup / Match per row 12 us per lookup at 1,000 rows, 0.55 ms at 100,000 Sorted keys: Match(k, keys, 1); above 250 lookups: a Dictionary Working with Excel
🟠 FileSystemObject created on every pass 7-10 us more per pass than one reused (6-10x) Create it once Calls, Classes and Binding
🟠 End(xlUp) in a loop condition 7 us per pass (230-360x a local copy) Read the last row once Working with Excel
🟠 WorksheetFunction in a loop 2.2 us per call (25-32x VBA) Compute in VBA Working with Excel
🟠 Calls through As Object 1.4-1.6 us per call (19x typed) Declare the type Calls, Classes and Binding
🟠 Any Declare API call 1.7 us per call (42x a VBA call) Keep out of hot loops Windows API on 64-bit Office
🟠 RegExp created per call 3.6 us per creation (61x Like) Create once, or Like / InStr Strings

Tip

The pattern behind every result: VBA time goes into copying large data, algorithms that grow with the square of the data, yielding to Windows, and crossing into Excel, COM objects or the Windows API (about 1-3 us per call; a line in the Immediate window 0.5-1.8 ms, a new Scripting.Dictionary 0.4 ms). Variants, Static, ParamArray, IIf, ReDim Preserve per item and plain call overhead cost nanoseconds to a fraction of a microsecond; they matter only in loops of millions.

πŸ“š Pages

Topic What it covers
πŸ“ Measurement Method How every figure was obtained, how far to trust it, how to repeat a run
πŸ“Š Working with Excel WorksheetFunction, DoEvents, progress output and loop conditions, cells and formatting, row deletes, lookups, Evaluate, calculation, volatile functions, Excel state
πŸ“¦ Passing Data ByVal and ByRef for arrays, strings, objects and class fields
πŸ”€ Strings Concatenation, scanning, Like against RegExp, number parsing
πŸ”’ Numbers and Dates Date functions, rounding, division, precision, powers
πŸ” Searching, Sorting and Collections Search, lookups, Collection against Dictionary, sorting, growing arrays
🧩 Calls, Classes and Binding Call cost, IIf, late and early binding, objects created in a loop, class traps, memory leaks
πŸ“„ Files and CSV Reading text files, UTF-8, CSV import and export
πŸͺŸ Windows API on 64-bit Office Declare cost, CopyMemory, waiting for processes, 64-bit rules
⚠️ Correctness Traps Every trap in one list
🧰 Skill Rules The checker rules PERF001-PERF025 and the skill's fix patterns, each marked πŸ“ measured or πŸ§ͺ to measure

πŸ”— Sources

  • Raw results and benchmark modules: docs/benchmarks/
  • Running record with every figure: docs/RECOMMENDATIONS.md
  • Release: these pages match the rules and severities of release v1.0.0 (PERF001-PERF025); see the releases for what changed since.
  • Part of the tips measured come from Francesco Balena, I trucchi di Visual Basic 6 (chapters 1-4, 9-11 and 15); the rest from the skill's own rules and from reviews of real VBA projects.

⚑ VBA Performance

Topics

Reference

Clone this wiki locally