OpenText

tuli1954

New Member
Joined
Apr 26, 2018
Messages
13
Hello,

I am trying to import a number of text files in one sheet.

If I am using the line below, a new workbook will open. How can I import a text file in an open excel workbook?

Workbooks.OpenText Filename:=fln, Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
Array(0, 1), Array(12, 1), Array(26, 1), Array(45, 1), Array(66, 1), Array(86, 1), Array( _
106, 1), Array(126, 1)), TrailingMinusNumbers:=True

Thank you
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You could try something like this,
- which places the data in the next available row (finding last occupied cell in column A) in SheetX
- multiple files can be selected from the same folder
- no attempt made to optimise the code (it is writing one row at a time to the worksheet)

VBA Code:
Sub ReadTextFile()
    Dim myFile As Variant, myFiles As Variant, text As String, textline As String, cel As Range
    myFiles = Application.GetOpenFilename(FileFilter:="Text Files (*.txt), *.txt", MultiSelect:=True)
    For Each myFile In myFiles
        Open myFile For Input As #1
        Do Until EOF(1)
            Line Input #1, textline
            Set cel = Sheets("SheetX").Cells(Rows.Count, 1).End(xlUp).Offset(1)
            cel.Value = textline
            cel.TextToColumns Destination:=cel, DataType:=xlFixedWidth, _
                FieldInfo:=Array(Array(0, 1), Array(12, 1), Array(26, 1), Array(45, 1), Array(66, 1), Array(86, 1), Array(106, 1), Array(126, 1)), _
                TrailingMinusNumbers:=True
        Loop
        Close #1
    Next myFile
End Sub
 
Upvote 0
hello, I get this error after 3 lines
1592668490396.png
 
Upvote 0
Something strange is going on in column A

Try this which uses a different method to determine next row

Delete prior code and replace with this
VBA Code:
Sub ReadTextFile()
    Dim myFile As Variant, myFiles As Variant, text As String, textline As String, cel As Range, ws As Worksheet
    Set ws = ActiveSheet
    myFiles = Application.GetOpenFilename(FileFilter:="Text Files (*.txt), *.txt", MultiSelect:=True)
    For Each myFile In myFiles
        Open myFile For Input As #1
        Do Until EOF(1)
            Line Input #1, textline
            Set cel = ws.Cells(Split(ws.UsedRange.Address, "$")(4) + 1, 1)
            cel.Value = textline
            cel.TextToColumns Destination:=cel, DataType:=xlFixedWidth, _
                FieldInfo:=Array(Array(0, 1), Array(12, 1), Array(26, 1), Array(45, 1), Array(66, 1), Array(86, 1), Array(106, 1), Array(126, 1)), _
                TrailingMinusNumbers:=True
        Loop
        Close #1
    Next myFile
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top