Hi Everyone,
I'm getting the VBA run-time error 6, "Overflow."
I've defined what I think are the applicable variables as long, and I think that should be enough for my purposes.
I have
and the problem is at the line where two 10-digit numbers are being added together:
This is when I get the overflow error.
How can I fix my code so that it doesn't crash? Thanks for your help.
Peter
Here's the complete code:
I'm getting the VBA run-time error 6, "Overflow."
I've defined what I think are the applicable variables as long, and I think that should be enough for my purposes.
I have
Code:
Dim dArray(0 to 9), mySum As Long
Code:
mySum = mySum + dArray(0) * 1000000000 + dArray(1) * _
100000000 + dArray(2) * 10000000 + dArray(3) * 1000000 _
+ dArray(4) * 100000 + dArray(5) * 10000 + dArray(6) * _
1000 + dArray(7) * 100 + dArray(8) * 10 + dArray(9)
How can I fix my code so that it doesn't crash? Thanks for your help.
Peter
Here's the complete code:
Code:
Sub generateList()
Dim myArray(0 To 9), dArray(0 To 9), digitsArray(0 To 9) As Long
Dim digitPosition As Long
Dim accumulatedValue, myFactorialArray(0 To 9), mySum As Long
myFactorialArray(0) = 1
digitsArray(0) = 0
mySum = 0
For j = 1 To 9
myFactorialArray(j) = j * myFactorialArray(j - 1)
Next j
For i = 0 To 3628799
For j = 0 To 9
digitsArray(j) = j
Next j
accumulatedValue = 0
For j = 0 To 9
digitPosition = Int((i - accumulatedValue) / _
myFactorialArray(9 - j))
dArray(j) = digitsArray(digitPosition)
accumulatedValue = accumulatedValue + digitPosition * _
myFactorialArray(9 - j)
For k = digitPosition To 8 - j
digitsArray(k) = digitsArray(k + 1)
Next k
Next j
If (((dArray(1) * 100 + dArray(2) * 10 + dArray(3)) Mod 2) = 0 _
) And (((dArray(2) * 100 + dArray(3) * 10 + dArray(4)) Mod _
3) = 0) And (((dArray(3) * 100 + dArray(4) * 10 + dArray(5 _
)) Mod 5) = 0) And (((dArray(4) * 100 + dArray(5) * 10 + _
dArray(6)) Mod 7) = 0) And (((dArray(5) * 100 + dArray(6) _
* 10 + dArray(7)) Mod 11) = 0) And (((dArray(6) * 100 + _
dArray(7) * 10 + dArray(8)) Mod 13) = 0) And (((dArray(7) _
* 100 + dArray(8) * 10 + dArray(9)) Mod 17) = 0) Then
mySum = mySum + dArray(0) * 1000000000 + dArray(1) * _
100000000 + dArray(2) * 10000000 + dArray(3) * 1000000 _
+ dArray(4) * 100000 + dArray(5) * 10000 + dArray(6) * _
1000 + dArray(7) * 100 + dArray(8) * 10 + dArray(9)
End If
If (i Mod 10000) = 0 Then
Cells(1, 10) = i
End If
Next i
Cells(1, 1) = mySum
End Sub