title: What does ReDim Preserve do?
body: <p>I am looking at someone else's vba excel code. they are doing <code>ReDim Preserve dataMatrix(7, i)</code> in both loops. What does this do?</p>
  
  <p>Also, it seems like the second loop just overwrites the data in the first loop, is that correct?</p>
  
  <pre><code>Dim dataMatrix() As String
  
      Worksheets.Item("ETS").Select
      Do While Trim(Cells(r, 1)) &lt;&gt; ""
         Debug.Print "The line: ", Trim(Cells(r, 1)), r
          r = r + 1
          dataMatrix(1, i) = Trim(Cells(r, 1))    ''file name
          dataMatrix(2, i) = Trim(Cells(r, 2))    ''sample type
          dataMatrix(3, i) = Trim(Cells(r, 3))    ''sample name
          dataMatrix(4, i) = "ETS"    ''
          dataMatrix(5, i) = Trim(Cells(r, 5))    ''Response
          dataMatrix(6, i) = Trim(Cells(r, 6))    ''ISTD Response
          dataMatrix(7, i) = Trim(Cells(r, 10))   ''Calculated Conc
          i = i + 1
          ReDim Preserve dataMatrix(7, i)
      Loop
  
      r = 5
      Worksheets.Item("ETG").Select
      Do While Trim(Cells(r, 1)) &lt;&gt; ""
         Debug.Print "The line: ", Trim(Cells(r, 1)), r
          r = r + 1
          dataMatrix(1, i) = Trim(Cells(r, 1))    ''file name
          dataMatrix(2, i) = Trim(Cells(r, 2))    ''sample type
          dataMatrix(3, i) = Trim(Cells(r, 3))    ''sample name
          dataMatrix(4, i) = "ETG"
          dataMatrix(5, i) = Trim(Cells(r, 5))    ''Response
          dataMatrix(6, i) = Trim(Cells(r, 6))    ''ISTD Response
          dataMatrix(7, i) = Trim(Cells(r, 10))   ''Calculated Conc
          i = i + 1
          ReDim Preserve dataMatrix(7, i)
      Loop
  </code></pre>