Oct 6th, 2009, 07:03 AM
#1
Thread Starter
Member
[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
Attached Files
Last edited by manin; Oct 6th, 2009 at 07:06 AM .
Oct 6th, 2009, 12:19 PM
#2
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
Oct 6th, 2009, 12:33 PM
#3
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear :
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Oct 6th, 2009, 01:44 PM
#4
Thread Starter
Member
Re: Import txt file in excel
thanks guys.
both codes work, but koolsid's is a bit better
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width