+ Reply to Thread
Results 1 to 9 of 9

Help Importing Multiple Text files into one Excel Sheet

  1. #1
    Registered User
    Join Date
    06-17-2013
    Location
    Lindale, TX
    MS-Off Ver
    Excel 2007
    Posts
    6

    Angry Help Importing Multiple Text files into one Excel Sheet

    I need help writing a loop to import test files from a folder. The text file are delimited. Any help would be appreciated. This is what I have so far

    Sub ReadFilesIntoActiveSheet()
    Dim fso As FileSystemObject
    Dim folder As folder
    Dim file As file
    Dim FileText As TextStream
    Dim TextLine As String
    Dim Items() As String
    Dim i As Long
    Dim cl As Range

    ' Get a FileSystem object
    Set fso = New FileSystemObject

    ' get the directory you want
    Set folder = fso.GetFolder("C:\Users\wtur\Desktop\June LogFiles\")

    ' set the starting point to write the data to
    Set cl = ActiveSheet.Cells(1, 1)

    ' Loop thru all files in the folder
    For Each file In folder.Files
    ' Open the file
    Set FileText = file.OpenAsTextStream(ForReading)

    ' Read the file one line at a time
    Do While Not FileText.AtEndOfStream
    TextLine = FileText.ReadLine

    ' Parse the line into | delimited pieces
    Items = .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(1, 1, 1, 1)
    .TextFileFixedColumnWidths = Array(3, 19, 3)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False

    ' Put data on one row in active sheet
    For i = 0 To UBound(Items)
    cl.Offset(0, i).Value = Items(i)
    Next

    ' Move to next row
    Set cl = cl.Offset(1, 0)
    Loop

    ' Clean up
    FileText.Close
    Next file

    Set FileText = Nothing
    Set file = Nothing
    Set folder = Nothing
    Set fso = Nothing

    End Sub

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Help Importing Multiple Text files into one Excel Sheet

    Maybe:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-17-2013
    Location
    Lindale, TX
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Help Importing Multiple Text files into one Excel Sheet

    First of all thanks for the reply. This loaded everything. But its space delimited and I didn't separate everything in to its own column.

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Help Importing Multiple Text files into one Excel Sheet

    Can you upload an example text file and desired result in a spreadsheet? When replying click go advanced and the attachments

  5. #5
    Registered User
    Join Date
    06-17-2013
    Location
    Lindale, TX
    MS-Off Ver
    Excel 2007
    Posts
    6

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Help Importing Multiple Text files into one Excel Sheet

    Maybe:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-17-2013
    Location
    Lindale, TX
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Help Importing Multiple Text files into one Excel Sheet

    Well it started to work then I received an error. Inserted page shots of the errors. Thanks for the help
    error message.JPGerror message2.JPG

  8. #8
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Help Importing Multiple Text files into one Excel Sheet

    Hard to know what the problem is from that. Do you have text files in different formats? What file was it trying to import? What was the value of split_line(0) at that point?

    The most likely issue is that the line was empty or something so split line is set to empty and split_line(0) doesn't exist. Try something like:
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    06-17-2013
    Location
    Lindale, TX
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Help Importing Multiple Text files into one Excel Sheet

    That worked Great. There was a space in my data that I never noticed. I apriciate all the help. Have a great day.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1