Skip to content

Correctness Traps

Daniele Penza edited this page Dec 1, 2026 · 3 revisions

⚠️ Correctness Traps

Scope Evidence

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.

πŸ“Š Excel and program flow

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 β€”

πŸ“¦ Parameters and classes

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

πŸ”’ Numbers, dates, text

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

πŸ“„ Files and memory

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 β€”

⚑ VBA Performance

Topics

Reference

Clone this wiki locally