Overflow when multiplying Integers and assigning to Long

calendar_today Asked Jun 26, 2013
thumb_up 16 upvotes
history Updated April 14, 2026

Direct Answer

This is a really odd VBA quirk. I'm amazed I've never bumped into this. Dim x As Long x = 24 * 60 * 60 ' Overflow x = 32767 + 1 ' Overflow. x = 32768 + 1 ' Works fine! So it looks…. This is a 5-line VBA Core snippet, ranked #34th of 95 by community upvote score, from 2013.


The Problem (Q-score 6, ranked #34th of 95 in the VBA Core archive)

The scenario as originally posted in 2013

If I type the following into the immediate window I get Runtime error ‘6’: Overflow.

MsgBox 24 * 60 * 60

Why is this?

This also fails:

Dim giveTime  As Long 
giveTime  = 24 * 60 * 60

Why is this? giveTime is declared as a Long type, so 24 × 60 × 60 = 86400 should comfortably fit.

Why community consensus is tight on this one

Across 95 VBA Core entries in the archive, the accepted answer here holds solid answer (above median) status — meaning voters are unusually aligned on the right fix.


The Verified Solution — solid answer (above median) (+16)

5-line VBA Core pattern (copy-ready)

This is a really odd VBA quirk. I’m amazed I’ve never bumped into this.

Dim x As Long
x = 24 * 60 * 60 ' Overflow
x = 32767 + 1 ' Overflow. 
x = 32768 + 1 ' Works fine!

So it looks like the * and + operators return an Integer in the first two examples. Sure enough, in the help file for the *operator (similar for the + operator):

result = number1 * number2

[…]

The data type of result is usually the same as that of the most precise expression.

Your literals 24, 60, and 60 are all of type Integer by default, so your * (or +) operator returns an Integer, which overflows because the result is greater than 32,767.

However, the literal 32,768 in the third example above defaults to Long type (since it is too large to be an Integer) and so the + returns a Long; no overflow.

The help file also says this:

If […] the data type of result is an Integer variant that overflows its legal range […] Then result is […] converted to a Long variant.

Emphasis mine. Now this little rule sounds like common sense, and anyone would reasonably assume that it applies in your case. But your numbers are of type Integer, not Variant/Integer, so VBA doesn’t apply this rule! Makes absolutely no sense to me, but that’s how it is, and that’s what the documentation says.

Solution: make one of the arguments of your * operator be of a type more precise than Integer (e.g. Long) and the problem will go away.

x = CLng(24) * 60 * 60 ' Result is Long, works fine.

In fact, and this is probably why I’ve never bumped into this quirk, I make a habit of declaring all of my Integer variables as Long instead, unless there is a specific concern that having Longs instead of Integers will cause problems with memory use or execution time (which is almost never the case). Granted, this won’t help in cases when you operate on literals smaller than 32,768, because they default to Integer type.


In your comment, you ask what a Variant/Integer is. Variant is basically a container type for any other data type. In the particular case where you make it contain an Integer:

Dim a As Variant ' a is now Empty
a = CInt(32767) ' a is now Variant/Integer
x = a + 1 ' works fine

But as noted above, a plain old Integer triggers the overflow error:

Dim b As Integer
b = 32767
x = b + 1 ' overflow


When to Use It — classic (2013–2016)

Ranked #34th in its category — specialized fit

This pattern sits in the 87% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the VBA Core archive for a higher-consensus alternative.

What changed between 2013 and 2026

The answer is 13 years old. The VBA Core object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

Is this above-median answer still worth copying?
expand_more

Answer score +16 vs the VBA Core archive median ~5; this entry is solid. The score plus 6 supporting upvotes on the question itself (+6) means the asker and 15 subsequent voters all validated the approach.

Does the 5-line snippet run as-is in Office 2026?
expand_more

Yes. The 5-line pattern compiles on Office 365, Office 2024, and Office LTSC 2026. Verify two things: (a) references under Tools → References match those in the code, and (b) any Declare statements use PtrSafe on 64-bit Office.

Published around 2013 — what’s changed since?
expand_more

Published 2013, which is 13 year(s) before today’s Office 2026 build. The VBA Core object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which VBA Core pattern ranks just above this one at #33?
expand_more

The pattern one rank above is “Embed an R process in a VBA macro”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 6, Answer-score 16, original post 2013, ranked #34th of 95 in the VBA Core archive. Last regenerated April 14, 2026.

vba