Repository navigation
Home
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.
| 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 |
| 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.
| 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.
| 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 |
| Every trap in one list | |
| π§° Skill Rules | The checker rules PERF001-PERF025 and the skill's fix patterns, each marked π measured or π§ͺ to measure |
- 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.
π Figures: Excel 365 (16.0 build 20723) 64-bit, Windows desktop, median of 5 runs Β· Raw data in docs/benchmarks/
- π Home
- π Measurement Method
Topics
- π Working with Excel
- π¦ Passing Data
- π€ Strings
- π’ Numbers and Dates
- π Searching, Sorting and Collections
- π§© Calls, Classes and Binding
- π Files and CSV
- πͺ Windows API on 64-bit Office
Reference
β οΈ Correctness Traps- π§° Skill Rules