1 Attachment(s)
[RESOLVED] Import txt file in excel
Hello, I am trying to import a txt file into excel 2003.
My problem is that i can't figure out how to make excel understand that the txt file is column based.
I want to import the data exactly as i see them in txt, as a table.
Code:
Sub OpenFile()
Dim A As Long, B As String, C As Variant, D As Variant, E As Variant
Dim iRow As Long
Dim i As Long
Dim Fname As Variant
Fname = Application.GetOpenFilename("Text Files (*.txt),*.txt", , _
"Select Text Data File")
If Fname = False Then Exit Sub
Open Fname For Input As #1
iRow = 1
i = 1
Do While Not EOF(1)
Input #1, A
Cells(1, i) = A
'iRow = iRow + 1
i = i + 1
Loop
Close 1
End Sub
I searched in here, but the other posts did not help me
Re: Import txt file in excel
Play around with this
Code:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 10/6/2009 by XXXXXXXXXXXXXX
'
'
With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\manin.txt" _
, Destination:=Range("A1"))
.Name = "manin"
.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 = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 1, 1, 1)
.TextFileFixedColumnWidths = Array(2, 8, 10)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Re: Import txt file in excel
Try this
vb Code:
Sub InmportTextFile()
Dim FlName As String
'~~> Replace with Actual Path and File name
FlName = "C:\MyFile.Txt"
Workbooks.OpenText Filename:=FlName, Origin:=437, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1), Array(5, 1)), TrailingMinusNumbers:=True
End Sub
Re: Import txt file in excel
thanks guys.
both codes work, but koolsid's is a bit better