Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Macro Run time error 6: overflow- coding inside a loop

Tags:

excel

vba

Having a problem with this Error. I am creating a GA and the loop is to assign my fitness value to an array.

some of the variables

Dim Chromolength as integer
Chromolength = varchromolength * aVariables 
Dim i as integer, j as integer, counter as integer
Dim Poparr() As Integer
Dim FitValarr() As Integer

the code:

ReDim Poparr(1 To PopSize, 1 To Chromolength)

For i = 1 To PopSize                
  For j = 1 To Chromolength       
    If Rnd < 0.5 Then           
        Poparr(i, j) = 0
    Else
        Poparr(i, j) = 1        
    End If
  Next j
Next i

For i = 1 To PopSize                   
 j = 1                           
 counter = Chromolength              
 Do While counter > 0  
   FitValarr(i) = FitValarr(i) + Poparr(i, counter) * 2 ^ (j - 1)          
  j = j + 1                   
  counter = counter - 1       
 Loop
Next i      

I am having problems with:

FitValarr(i) = FitValarr(i) + Poparr(i, counter) * 2 ^ (j - 1) 

I apologize, I am fairly new to VBA.

like image 243
John Hopley Avatar asked Sep 25 '12 17:09

John Hopley


People also ask

How do I fix error 6 overflow?

You just need to restart the computer to start the computer normally.

How do I fix error 6 in VBA?

The data type Byte can hold values from 0 to 255. So it causes an error. To fix the error, we either change the data type or reduce the value assigned to the variable “Number.”

What is Run Time error 6 Overflow?

This error has the following causes and solutions: The result of an assignment, calculation, or data type conversion is too large to be represented within the range of values allowed for that type of variable. Assign the value to a variable of a type that can hold a larger range of values.


1 Answers

An overflow condition arises when you create an integer expression that evaluates to a value larger than can be expressed in a 16-bit signed integer. Given the expression, either the contents of FitValarr(i), or the expression 2^(j-1) could be overflowing. Suggest all the the variables presently declared as Int be changed to Long. Long integers are 32-bit signed values and provide a correspondingly larger range of possible values.

like image 170
David W Avatar answered Nov 11 '22 13:11

David W