Rijnsent
Well-known Member
- Joined
- Oct 17, 2005
- Messages
- 1,424
- Office Version
- 365
- Platform
- Windows
I'm a bit stumped: why does VBA behave like this? See the Sub below: when I simply Debug.Print 60*24*2*60, I get an Overflow error. So apparently VBA treats that multiplication as an integer and therefore fails as it's too big. But why do the tests after that fail and why do the last two succeed? I mean: I'm happy my code works again, but I don't get why the one Overflows and the other doesn't.
Anybody a clue?
Thanks,
Koen
Anybody a clue?
Thanks,
Koen
Code:
Sub TestOverflow()
Dim LimitTime As Double
Dim AddTime As Double
On Error Resume Next
'Test 1
Debug.Print 60 * 24 * 2 * 60
Debug.Print 1, Err.Number, Err.Description
Err.Clear
'Overflow, apparently the Debug.Print can only handle an integer (-32k to +32k)
'Test 2
AddTime = (60 * 24 * 2 * 60)
Debug.Print AddTime
Debug.Print 2, Err.Number, Err.Description
Err.Clear
'Overflow, although AddTime is declared
'Test 3
AddTime = CDbl(60 * 24 * 2 * 60)
Debug.Print AddTime
Debug.Print 3, Err.Number, Err.Description
Err.Clear
'Overflow, although forcing a double, also seen as integer
'Test 4
LimitTime = 1234567890
LimitTime = LimitTime - ((60 * 24) * 2 * 60)
Debug.Print LimitTime
Debug.Print 4, Err.Number, Err.Description
Err.Clear
'Overflow, although there is a value again the logic seems to transform the multiplication into an integer
'Test 5
bla = 60
Debug.Print bla * 24 * 2 * 60
Debug.Print 5, Err.Number, Err.Description
Err.Clear
'NO ERROR, why?
'Test 6
blaTwo = 2
Debug.Print 1234567890 - 60 * 24 * blaTwo * 60
Debug.Print 6, Err.Number, Err.Description 'NO ERROR
Err.Clear
'NO ERROR, why?
On Error GoTo 0
End Sub