溢出运行时错误6 VBA

时间:2017-10-01 08:25:21

标签: excel-vba vba excel

我想知道我在代码中做错了什么。我已经将所有内容都更改为Long,但仍然出现溢出错误。请帮忙。这一行:

Binary = Binary + Generation(i, j) * 10 ^ (j - 1)

已突出显示黄色。

完整代码:

Dim Generation() As Long
Dim Fitness() As Long
Dim i, j As Long
Dim Binary As Long
Dim Binary2 As Long
Dim Initial As Long
Dim x, y As Long

Private Sub ButtonGeneratePopulation_Click()

Initial = Val(InitialPopulation.Value)

ReDim Generation(Initial, 30) As Long
Randomize

For i = 1 To Initial
    For j = 1 To 30
        If Rnd > 0.5 Then
            Generation(i, j) = 1
        Else
            Generation(i, j) = 0
        End If
    Next j
Next i



For i = 1 To Initial
Binary = 0
    For j = 1 To 30
        Binary = Binary + Generation(i, j) * 10 ^ (j - 1)
    Next j
    Cells(i, 1) = Binary
Next i

InitialPopulation.Enabled = False

End Sub

1 个答案:

答案 0 :(得分:0)

Dim Generation() As Long
Dim Fitness() As Long
Dim i As Long, j As Long
Dim Binary As Double '<~~~ this is double
Dim Binary2 As Long
Dim Initial As Long
Dim x As Long, y As Long

Private Sub ButtonGeneratePopulation_Click()

Initial = Val(InitialPopulation.Value)



ReDim Generation(Initial, 30) As Long
Randomize

For i = 1 To Initial
    For j = 1 To 30
        If Rnd > 0.5 Then
            Generation(i, j) = 1
        Else
            Generation(i, j) = 0
        End If
    Next j
Next i



For i = 1 To Initial
Binary = 0
    For j = 1 To 30
        Binary = Binary + Generation(i, j) * 10 ^ (j - 1)
    Next j
    Cells(i, 1) = Binary
Next i

InitialPopulation.Enabled = False

End Sub