I've been developing a macro that needs to put values into a one-dimensional array for filtering a table by month. In some places, my code works and in others, the same piece of code returns an Overflow Error. The problem occurs when I divide the loop counter by two. This division returns the Overflow error.
I have distilled the problem code into a separate macro in a new workbook and I am still getting the error. The macro in its entirety is:
The error occurs at the line
Debug.Print "j divided by 2 is " & j / 2
For some reason, dividing the even integer j by 2 returns an overflow error.
In this macro, the maximum value of j is 6, so this should not cause an integer variable to overflow. Only even numbers are divided by 2, so the result should always be an integer.
Things I have already tried:
1. Declaring j as types other than Integer: Long, Double,
2. Changing the Debug.Print line to print Int(j / 2)
3. Changing the Debug.Print line to print Int(j) / 2
4. Leaving j as Integer and converting the value in j to a new variable, i, which is Long or Double
Anyone any idea why this error is occurring and how to solve it?