title: Calculate variance for array in VBA
body: <p>I have a trouble with VBA. I need to calculate variance for array. But array in for loops, and if I use <code>Next i</code> it will increment <code>i</code> by 1. So I always have to face with error messages from VBA.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>Function cal_var(dta As Variant)
  Dim N, i, j As Integer
  Dim tre() As Double
  N = UBound(dta)
  Dim vec_var() As Integer
  ReDim vec_var(1 To N)
  For i = 1 To N
      j = 1
      ReDim tre(1 To i)
      For j = 1 To i
          tre(j) = dta(j)
      Next j
      vec_var(i) = Application.WorksheetFunction.Var_P(tre)
  Next i
  cal_var = vec_var
  End Function
  </code></pre>
  
  <p>The following <code>Sub</code> checks my function</p>
  
  <pre><code>Sub test()
  Dim b(1 To 5) As Integer
  Dim a As Double
  b(1) = 1
  b(2) = 2
  b(3) = 3
  b(4) = 4
  b(5) = 5
  a = cal_var(b)
  MsgBox a
  End Sub
  </code></pre>