title: VBA Macro Run-time error 9: subscript out of range - if statement
body: <p>I am working on a project where i need to use a GA. I have encountered the following Error in my code. </p>
  
  <p>The following code is to do the crossover of the GA. Most of the variables are entered through an userform. *Note this code may contain more errors.</p>
  
  <p>My variables:</p>
  
  <pre><code>Dim FitValarr() As Long
  Dim fitsmall As Long
  Dim Poparr() As Integer
  Dim PopSize As Long
  Dim Cross_Prob As Double
  Dim overallRandom As Single
  Dim FitValarr() As Long
  Dim fitsmall As Long
  Dim newChildarr() As Integer
  Dim newChildfit As Integer
  Dim child1() As Integer
  Dim child2() As Integer
  Dim fitvalchild1 As Integer
  Dim fitvalchild2 As Integer
  Dim crossoverpos As Integer
  Dim chromosomerandom As Integer
  Dim fitbig As Integer
  Dim i As Long, j As Long, counter As Long
  Dim Chromolength As Integer
  </code></pre>
  
  <p>the code:</p>
  
  <pre><code>Chromolength = varchromolength * aVariables  'usual value is around 30 = (10*3)
  ReDim Poparr(1 To PopSize, 1 To Chromolength)
  ReDim FitValarr(1 To PopSize)                 'popsize is around 10 -50
  ReDim newChildarr(1 To Chromolength) As Integer
  ReDim child1(1 To Chromolength) As Integer
  ReDim child2(1 To Chromolength) As Integer
  
   'chosing two random chromosomes
  Dim rand1 As Integer
  Dim rand2 As Integer
  
  rand1 = Int(Rnd * PopSize)               'generate random value between 1-50
  rand2 = Int(Rnd * PopSize)               'same as above
  
  Do While rand1 = rand2
         rand2 = Int(Rnd * PopSize)        'checking that the two random values isn't the same
  Loop
  
  
  If overallRandom &lt; Cross_Prob Then                  
  
      If FitValarr(rand1) &gt; FitValarr(rand2) Then    
          Do While rand1 = rand2
          rand2 = Int(Rnd * PopSize)
          Loop
      Else
          Do While rand1 = rand2                     
          rand1 = Int(Rnd * PopSize)
          Loop
      End If
  
      crossoverpos = Int(Rnd * Chromolength)
      Do While crossoverpos &lt; 1 &amp; crossoverpos &gt; Chromolength
      Loop
  
      'do crossover and assign it to child# array
      For i = 1 To crossoverpos                                   
          child1(i) = Poparr(rand1, i)                            
          child2(i) = Poparr(rand2, i)                            
      Next i
  
      For i = crossoverpos To Chromolength
          child1(i) = Poparr(rand2, i)                            
          child2(i) = Poparr(rand1, i)                            
      Next i
  
      'fitness of the two offsprings
      For i = 1 To Chromolength                                  
          counter = Chromolength
  
          Do While counter &gt; 0
              fitvalchild1 = fitvalchild1 + child1(counter) * 2 ^ (i - 1)
              fitvalchild2 = fitvalchild2 + child2(counter) * 2 ^ (i - 1)
              counter = counter - 1
          Loop
      Next i
  
      If fitvalchild1 &gt; fitvalchild2 Then                        
          newChildfit = fitvalchild1                             
          For i = 1 To Chromolength
          newChildarr(i) = child1(i)                             
          Next i
  
      Else                                                       
          newChildfit = fitvalchild2                             
          For i = 1 To Chromolength
          newChildarr(i) = child2(i)                             
          Next i
  
      End If
  
  Else                                                            
      newChildfit = FitValarr(rand1)                              
      For i = 1 To Chromolength
          newChildarr(i) = Poparr(rand1, i)                       
      Next i
  
  End If 'end crossover
  </code></pre>
  
  <p>the error is in the crossover code:</p>
  
  <pre><code> If FitValarr(rand1) &gt; FitValarr(rand2) Then 
  </code></pre>
  
  <p>I apologise for the long code. I am fairly new to VBA and still struggeling bit.</p>