Repository navigation
Correctness Traps
Daniele Penza edited this page Dec 1, 2026
·
3 revisions
A faster form is worth nothing if it changes a result, an error, or Excel's state. Every trap below either changes results silently or leaves Excel in a wrong state. π marks traps reproduced in Excel 365 64-bit.
| Trap | What happens | Page |
|---|---|---|
End |
Skips error handlers: Calculation, ScreenUpdating, EnableEvents stay off |
Working with Excel |
π A missing Set
|
v = Range("A1") copies the default value silently |
Working with Excel |
Debug.Assert False left in code |
Stops the user's macro in the editor | Working with Excel |
π .Value on a currency cell |
Rounded to 4 decimals: 1.23456 β 1.2346; .Value2 is exact |
Working with Excel |
π Writing a block back with .Value
|
Formulas in the block become values | Working with Excel |
| π A one-cell range |
.Value2 returns a scalar, not an array |
Working with Excel |
| π Deleting rows top-down | Skips the row after each delete | Working with Excel |
π SpecialCells with nothing to return |
Error 1004 | Working with Excel |
π Application.Evaluate and [A1]
|
Read the active sheet, not the one the code means | Working with Excel |
π Approximate lookups (VLookup(..., True), Match(..., 1)) |
Need keys sorted ascending: on descending keys VLookup returned #N/A
|
Working with Excel |
π Range.Calculate
|
Dependants outside the range keep their old values | Working with Excel |
π Worksheet.Calculate
|
Volatile cells on other sheets stay stale | Working with Excel |
| π A change handler after a switch to block writes | One event for the whole block: Target holds every cell |
Working with Excel |
And / Or do not short-circuit |
If i <= UBound(a) And a(i) > 0 still evaluates a(i): use nested If
|
β |
π IIf, Choose, Switch evaluate every argument |
Side effects and errors in every branch: IIf(y = 0, 0, 1 / y) raises error 11 |
Calls, Classes and Binding |
| π A loop bound copied to a local |
Do While i <= UBound(a) sees the array grow in the loop; the copy and a For bound do not |
Working with Excel |
Dim inside a loop |
Does not reset the variable on each pass: removing x = 0 changes results |
β |
Copying a Static or field to a local for a hot loop |
Write it back after the loop and on the error path | β |
| Trap | What happens | Page |
|---|---|---|
π A class's Public field passed ByRef
|
The callee changes a copy | Passing Data |
ByVal on an object |
The callee can still change its properties | Passing Data |
Typing an Optional parameter (As Long = -1) |
IsMissing is then always False
|
β |
π Dim x As New C
|
x Is Nothing is always False
|
Calls, Classes and Binding |
| π Circular references |
Class_Terminate never runs; memory leaks |
Calls, Classes and Binding |
π One Dictionary reused with RemoveAll
|
Keeps its settings: a text CompareMode set for one pass applies to every later pass |
Calls, Classes and Binding |
| Trap | What happens | Page |
|---|---|---|
π CLng, CInt, Round round half to even |
Differ from Excel's ROUND at exact halves |
Numbers and Dates |
π Half-away rounding in Double
|
2.675 rounds to 2.67 | Numbers and Dates |
π \ against Int against CLng
|
Different results on negatives and halves | Numbers and Dates |
π Double precision |
1E18 + 1 = 1E18; 0.1 + 0.2 <> 0.3
|
Numbers and Dates |
| Literal overflow |
60 * 60 * 24 raises error 6 |
Numbers and Dates |
And / Or / Xor on non-integers |
Operands are rounded to Long first |
β |
π CDbl on text |
Follows the locale: CDbl("1.5") = 15 in Italian |
Strings |
π Val on text |
Stops at a comma: Val("1,5") = 1
|
Strings |
π IsNumeric
|
Accepts "1e5", "(5)", "5-", "&H10", currency signs |
Strings |
π Left$ on Null
|
Raises error 94 where Left returns Null
|
Strings |
π Like and letters |
Case-sensitive by default; [A-Za-z] misses accented letters |
Strings |
RegExp pattern from `Join(words, " |
")` | Special characters change the pattern |
| Trap | What happens | Page |
|---|---|---|
| π UTF-8 read as ANSI | One accented letter becomes two characters | Files and CSV |
π Invalid UTF-8 through ADODB.Stream
|
No error; the text is cut at the bad bytes | Files and CSV |
π Text written through Range.Value
|
US rules whatever the locale: "007" becomes 7, "1234.56" a number, and a cell keeps the previous date format |
Files and CSV |
Input$ |
Stops at Ctrl-Z | Files and CSV |
π ReDim Preserve
|
Changes only the upper bound of the last dimension: error 9 otherwise | Searching, Sorting and Collections |
32-bit Declare and CopyMemory code |
Wrong memory on 64-bit: Excel closes | Windows API on 64-bit Office |
RtlZeroMemory or CopyMemory on non-numeric arrays |
Corrupts memory | Windows API on 64-bit Office |
Waiting on a process started with Shell
|
The process may end before OpenProcess
|
Windows API on 64-bit Office |
Join membership test |
False match when an element contains vbNullChar
|
Searching, Sorting and Collections |
Multi-key sort through Format$ keys |
Negative numbers sort wrongly as text | β |
π 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