I create a function to sum all the cells within a range as following
Function SumTest(rg As Range) As Double
Debug.Print "SumTest..."
Dim s As Double
s = 0 ' ------> Show runtime error "Overflow" at this line
Dim i As Range
For Each i In rg.Cells
s = s + i.Value2
Next i
SumTest = s
End Function
When I run the function at the immediate window it complains runtime error "Overflow", and it's very weird that the error message disappears after commenting the "Debug.Print" line. Does the function Dbbug.Print relate to double variable initialization? And why does the overflow happen here (I just assign the zero to a variable with Double data type)?
More information added:
I test the function at the immediate window as ?Sumtest(Range("A1:C1")) (The range A1:C1 is filled with trivial test data 1 2 3). And the version of Excel is Office365 on Mac.
To simplify the test, I use another shorter program like this:
Function SumTest2() As Double
Dim s As Double
Debug.Print "SumTest2"
s = 0 ' ---> Still "Overflow" here
SumTest2 = s
End Function
After having testing a few programs I am considering this maybe a bug for Excel 2016 on Mac. It seems once you using Debug.print then you cannot assign value (0 or other value) to a variable with Double data type (Integer, String or other data types are ok..) any more. Following is another typical test program:
Sub DoubleTest2()
Dim a As Double
a = 0
Debug.Print a ' when this line appears, next assignment to variable `b` will complain `overflow` error message.
Dim b As Double
b = 100# ' *overflow error message*
Debug.Print b
End Sub
=SumTest2()everything goes well. It only happens when using immediate window to run function..