Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import and transpose multiple txt files 1

Status
Not open for further replies.

SurfdudeNL

Programmer
Oct 11, 2003
13
NL
Hi,
I have multiple .txt files, each containing 3 lines of variable lengts. I want to import them into Excel and make every line a database record (transpose?).

.txt file example:
John
Pete Sampras
Ike Turner

Excel should look like this:
John Pete Sampras Ike Turner
etc.

Any suggestions?
 
hi SurfdudeNL

You read the file to EOF into an array that you ReDim Preserve with each read.

Then write the array to the next available row.

:)

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884

Skip,
 
Thanks Skip,

I'm not that familiar with array's can you help me with some code?
 
Ah no,

In the past I accidentally signed up as a programmer, I am certainly not one. The proof: I don't even know how to change this into something more appropriate ;-)

Arnie
 
Arnie,
Code:
Option Base 1
Sub Main()
    Dim fs, f, fl, fc, Recs()
    fn = Application.GetOpenFilename
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(ParsePath(fn))
    Set fc = f.Files
    For Each fl In fc
        Open fl For Input As #1
        i = 1
        Redim Recs(i) 
        Do While Not EOF(1)
           Redim Preserve Recs(i)
           Line Input #1, Recs(i)
           i = i + 1
        Loop
        Close #1
        for i = 1 to ubound(recs, 1)
           with Sheet1
             r = .cells(1,1).currentregion.rows.count + 1
             .Cells(r, i).Value = recs(i)
           end with
        next
    Next
End Sub
Make sure that you put a row of headings in row 1 before running this code.

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884

Skip,
 
Thanks, but I get an ActiveX-... can't make object error on the CreateObject command.

What's wrong?
 
Vis Basic For apps
Ms Excel 11.0 Object lib
OLE autom

Thats all
 
So sorry -- I did not post the function
Code:
Function ParsePath(fn)
    Dim i, s
    For i = Len(fn) To 1 Step -1
        s = Mid(fn, i, 1)
        Select Case s
        Case "\"
            ParsePath = Left(fn, i - 1)
            Exit Function
        End Select
    Next
End Function
[blush]

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884

Skip,
 
send your workbook to

skip @ theofficeexperts.com

and remove spaces in e mail address

I will take a look in about 2 hours. :)

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884

Skip,
 
Seems you don't have WSH installed.

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top