Import a file : Text to Excel « Excel « VBA / Excel / Access / Word






Import a file

 
Option Explicit

Sub ImportDemo()
  Dim fname$
  Dim newworkb As Workbook
  fname = ThisWorkbook.Path + "\file.txt"
  Workbooks.OpenText Filename:=fname, _
    Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
    Tab:=True, Semicolon:=False, Comma:=False, Space:=False, _
    Other:=False, DecimalSeparator:=".", ThousandsSeparator:=" ", _
    FieldInfo:=Array(Array(1, 1), Array(2, 3), Array(3, 1), Array(4, 1))
  Set newworkb = ActiveWorkbook
  newworkb.Worksheets(1).Copy after:=ThisWorkbook.Worksheets(1)
  newworkb.Close False
End Sub

 








Related examples in the same category

1.Excel has built-in options to read files where fields are delimited by tabs, semicolons, commas, or spaces.
2.Opening Delimited Files Simplified
3.Opening Fixed-Width Files