title: Excel VBA - Extract lines containing a particular name from multiple text files
body: <p>Attempting to extract text lines from text files to excel, but only if they contain a certain name within the line. Ignoring the rest of the lines.</p>
  
  <p>I'm currently working with a large number of text files containing standardized information for certain products. Each file has the same list of products, but information from different dates. I've put together some code that pulls in each row from the text files contained with the directory, and includes them together in a single excel spreadsheet.</p>
  
  <p>What I would like to change is for the code to only copy over text lines containing a certain name within the line like "Large Cap Index". The rest of the data can be ignored. This is attempting to cut down on the amount of time it takes to pull in the data, since only about 5% of the lines within each text file is needed.</p>
  
  <pre><code>Sub ReadFilesIntoActiveSheet()
      Dim fso As FileSystemObject
      Dim folder As folder
      Dim file As file
      Dim FileText As TextStream
      Dim TextLine As String
      Dim Items() As String
      Dim i As Long
      Dim cl As Range
      ' Get a FileSystem object
      Set fso = New FileSystemObject
      ' get the directory you want
      Set folder = fso.GetFolder("C:\Users\crowe12\Desktop\Projects\CRSP\Test")
      Set cl = ActiveSheet.Cells(1, 1)
      For Each file In folder.Files
          Set FileText = file.OpenAsTextStream(ForReading)
          Do While Not FileText.AtEndOfStream
              TextLine = FileText.ReadLine
              Items = Split(TextLine, "|")
              cl.Value = folder &amp; "\" &amp; file.Name
              For i = 0 To UBound(Items)
                  cl.Offset(0, i + 1).Value = Items(i)
              Next
              Set cl = cl.Offset(1, 0)
          Loop
          FileText.Close
      Next file
      Set FileText = Nothing
      Set file = Nothing
      Set folder = Nothing
      Set fso = Nothing
  End Sub
  </code></pre>