Skip to content

Numbers and Dates

Daniele Penza edited this page Dec 1, 2026 · 1 revision

πŸ”’ Numbers and Dates

Evidence Locale

🟠 Functions that take an interval or format string

πŸ“ DateDiff, DatePart and Format$ parse their string argument on every call: 0.3-0.9 us, 3-14x the direct form.

Task (1,000,000 calls) ❌ Slow form βœ… Fast form Ratio
Compare two whole dates DateDiff("d", d1, d2) = 0 d1 = d2 13.7x
Days in a month DateDiff of two DateSerial Select Case plus the leap rule 9.6x
Leap year IsDate("2/29/" & y) Mod 4 / 100 / 400 7.8x
Weekday DatePart("w", d, vbMonday) Weekday(d, vbMonday) 7.8x
Key from a date Format$(d, "yyyy-mm-dd") CStr(CLng(d)) 5.7x
Days to year end DateDiff("d", d, DateSerial(y, 12, 31)) DateSerial(y, 12, 31) - d 4.4x
Year DatePart("yyyy", d) Year(d) 3.0x
Public Function IsLeapYear(ByVal y As Long) As Boolean
    IsLeapYear = ((y Mod 4 = 0) And ((y Mod 100 <> 0) Or (y Mod 400 = 0)))
End Function

Public Function DaysInMonth(ByVal y As Long, ByVal m As Long) As Long
    Select Case m
        Case 1, 3, 5, 7, 8, 10, 12: DaysInMonth = 31
        Case 4, 6, 9, 11: DaysInMonth = 30
        Case 2: If IsLeapYear(y) Then DaysInMonth = 29 Else DaysInMonth = 28
    End Select
End Function

Note

The day-0 idiom Day(DateSerial(y, m + 1, 0)) is 2x the Select Case form and raises error 5 at the calendar's upper limit (DateSerial(9999, 13, 0)).

🎯 Rounding like Excel

CLng, CInt and VBA Round round half to even (banker's rounding); Excel's ROUND rounds half away from zero.

Expression πŸ“ Result
CLng(2.5), CLng(3.5) 2, 4
Round(2.5), Round(-2.5), Round(0.5) 2, -2, 0
WorksheetFunction.Round(2.5, 0) 3
Round(2.675, 2), WorksheetFunction.Round(2.675, 2) 2.68, 2.68
' βœ… matched WorksheetFunction.Round on all 200,000 values tested
'    (3-decimal inputs rounded to 2 digits), at 3.7x its speed
Public Function RoundLikeExcel(ByVal x As Double, ByVal Digits As Long) As Double
    Dim f As Double
    f = 10 ^ Digits
    RoundLikeExcel = CDbl(Sgn(x) * Int(CDec(Abs(x)) * f + 0.5) / f)
End Function
Rounding 200,000 values to 2 decimals Per call Matches Excel
VBA Round (half to even) 86 ns ❌ differs at exact halves
WorksheetFunction.Round 2,164 ns βœ…
Half away in Double: Sgn(x) * Int(Abs(x) * f + 0.5) / f 98 ns ❌ 2.675 becomes 2.67
Half away through CDec (above) 588 ns βœ…

Warning

Test RoundLikeExcel on your own value range before relying on it: the 200,000-value check covers 3-decimal inputs rounded to 2 digits.

⚠️ Division, precision, overflow

Trap πŸ“ Evidence What to do
\, Int(a / b) and CLng(a / b) differ -7 \ 2 = -3, Int(-7 / 2) = -4, CLng(7 / 2) = 4, CLng(5 / 2) = 2 Swap them only for non-negative operands and exact divisions
Double has about 15-16 digits 1E18 + 1 = 1E18; 0.1 + 0.2 <> 0.3 Currency or Decimal for sums that must reconcile to the cent
Literal arithmetic overflows Const SECONDS = 60 * 60 * 24 raises error 6: small literals are Integer Write 60& * 60 * 24
Timer at midnight Returns to 0: a wait loop on Timer may never end Application.Wait, or a counter

🟒 Small or negligible

Advice πŸ“ Result
a \ b instead of a / b into a Long 1.5x, about 12 ns; semantics differ (above)
A lookup table instead of 2 ^ n 3.7x, about 60 ns
Exp(t * Log(1 + r)) or repeated multiplication instead of (1 + r) ^ t ^ is the fastest; Exp/Log equal within 1 ulp; repeated multiplication 2.2x slower
Long instead of Integer for speed Integer was 10% faster; use Long anyway: Integer overflows at 32,767
Typed counters instead of Variant Not slower in a simple numeric loop (test to repeat)
Static or module variable instead of a local 1.2x, about 3 ns

⚑ VBA Performance

Topics

Reference

Clone this wiki locally