title: Copy specific lines from text files into excel
body: <p>I have developed the below code to open a large number of text files (within the same folder) and copy everything from each file into excel (one cell for each line of text file &amp; one row for each text file).</p>
  
  <p>However, I do not require all of the data from the text files and it is slowing down the process. The text files are in following format:</p>
  
  <pre><code>DATASET UNSTRUCTURED_GRID
  POINTS 5 float
  0.096853 0.000000 0.111997
  0.096853 -0.003500 0.111997
  0.096890 0.000000 0.084015
  0.096853 -0.003500 0.111997
  0.096890 -0.003500 0.084015
  CELL_DATA 5
  SCALARS pressure float 1
  LOOKUP_TABLE default
  -0.000000
  -0.000000
  -3.000000
  -2.000000
  -6.000000
  </code></pre>
  
  <p>The data that I need to copy from this file is the second batch of numbers (below "LOOKUP_TABLE default"). The number of lines in this example is five (as stated on line starting "CELL_DATA" but this number can change from file to file.</p>
  
  <p>In summary, I'm looking my code to only copy this last batch of numbers into excel instead of everything but I'm at a loss on how to tackle this.</p>
  
  <p>Any help or advice would be greatly appreciated.</p>
  
  <p>Sub ImportTextFile()</p>
  
  <pre><code>Dim RowNdx As Integer
  Dim ColNdx As Integer
  Dim TempVal As String
  Dim WholeLine As String
  Dim Pos As Integer
  Dim NextPos As Long
  Dim SaveColNdx As Integer
  
  FName = "E:\zdump\"
  MyFile = Dir(FName &amp; "*.txt")
  Sep = vbLf
  
  SaveColNdx = ActiveCell.Column
  RowNdx = ActiveCell.Row
  
  Do While MyFile &lt;&gt; ""
      Open (FName &amp; MyFile) For Input As #1
  
      While Not EOF(1)
          Line Input #1, WholeLine
          If Right(WholeLine, 1) &lt;&gt; Sep Then
              WholeLine = WholeLine &amp; Sep
          End If
          ColNdx = SaveColNdx
          Pos = 1
          NextPos = InStr(Pos, WholeLine, Sep)
          While NextPos &gt;= 1
              TempVal = Mid(WholeLine, Pos, NextPos - Pos)
              Cells(RowNdx, ColNdx).Value = TempVal
              Pos = NextPos + 1
              ColNdx = ColNdx + 1
              NextPos = InStr(Pos, WholeLine, Sep)
          Wend
          RowNdx = RowNdx + 1
      Wend
      Close #1
      MyFile = Dir()
      Debug.Print text
  Loop End Sub
  </code></pre>