title: importing multiple text files into separate Excel sheets in 1 Excel book using VBA
body: <p>I have written a VBA macro for importing many text files (from 1 folder) onto separate sheets in 1 Excel workbook.  All files read into each separate sheet just fine.  However, I am seeing a field placement issue.  The headers on each text file are the same.  But, the field values themselves sometimes get pushed down by a few fields.  So, not all fields values line up under their proper headers.  Can anybody suggest to me why this is happening?  I have tried seeing if it's a tab-delimited or a pipe-delimited issue, but that doesn't seem to be the problem.</p>
  
  <pre><code>Sub MultipleTextFilesIntoExcelSheets()
      Dim i As Integer 'a counter to loop through the files in the folder
      Dim fname As String, FullName As String 'fname is the name of the file, and FullName is the name of its path
      Dim ws As Worksheet 'a workbook object for the workbook where the current macro is running
  
  i = 0 'seed the counter
  
  'get the name of the first text file
  fname = Dir("C:\dummy_path\*txt")
  
  'loop through the text files to put them onto separate sheets in the Excel book
  While (Len(fname) &gt; 0)
      'get the full path of the text file
      FullName = "C:\dummy_path\" &amp; fname
      i = i + 1 'get ready for the next iteration
  
      Set ws = ThisWorkbook.Sheets("Sheet" &amp; i) 'the current sheet
  
      With ws.QueryTables.Add(Connection:="TEXT;" &amp; FullName, Destination:=ws.Range("A1"))
          .Name = "a" &amp; i
          .FieldNames = True
          .RowNumbers = False
          .FillAdjacentFormulas = False
          .PreserveFormatting = True
          .RefreshOnFileOpen = False
          .RefreshStyle = xlInsertDeleteCells
          .SavePassword = False
          .SaveData = True
          .AdjustColumnWidth = True
          .RefreshPeriod = 0
          .TextFilePromptOnRefresh = False
          .TextFilePlatform = 437
          .TextFileStartRow = 1
          .TextFileParseType = xlDelimited
          .TextFileTextQualifier = xlTextQualifierDoubleQuote
          .TextFileConsecutiveDelimiter = False
          .TextFileTabDelimiter = True    'we are using a tab-delimited file
          .TextFileSemicolonDelimiter = False
          .TextFileCommaDelimiter = False
          .TextFileSpaceDelimiter = False
          .TextFileOtherDelimiter = False
          .TextFileColumnDataTypes = Array(1, 1, 1)
          .TextFileTrailingMinusNumbers = True
          .Refresh BackgroundQuery:=False
          fname = Dir
      End With
  Wend
  </code></pre>
  
  <p>End Sub</p>