I have some vba behaviour that I do not understand. I'm running vba in a macro of excel 2016.
Sub alpha()
Dim a As Integer, b As Long
a = 750
b = 50 * a
Stop
End Sub
Running this sub results in an overflow error. Why?
Variable a is an integer and therefore cannot hold the number 750*50 because that is too high. But variable b is dimensionalised as a long - so it should be able to hold that number.
Strangely - If I change the type of variable a to Long then the code runs through.
Dim a As IntegertoDim a As Longb = 50 * cLng(a): VBA is not doing an implicit conversion from Integer to Long until after it has done the calculation.Longinstead ofIntegerin VBA because there is no benefit in usingIntegerat all.