+ Reply to Thread
Results 1 to 4 of 4

Folder of text files into excel sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    09-24-2014
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2013
    Posts
    2

    Folder of text files into excel sheet

    Hi,

    I need to import a folder of text files (thousands) into excel, so that the filename makes one column and the contents of the text file makes one column.

    So, a file named 12345.txt containing the text "Here's the text in file 12345.txt" and a file named 23456.txt containing the text "And here's the text in file 23456.txt", would give me this:

    A B
    1 12345.txt Here's the text in file 12345.txt
    2 23456.txt And here's the text in file 23456.txt


    I would be very greatful if anybody could give me a pointer on how to solve this. I don't know if Excel alone can do it, or if I should start by merging all the text files to one file (how would I get the file names into the final file?), but I'm hoping I'm not the first person to have this problem ;)

    Thanks,

    Pål

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Folder of text files into excel sheet

    Not tested.
    Sub GetData2()
        Dim WS As Worksheet, NextRow As Long
        Dim Data() As Byte
        Dim FolderPath As Variant, Lines As Variant, oFile As Variant
        Dim oFiles As Object, oFolder As Object, oShell As Object
        Dim Text As String
    
        Set WS = Worksheets("Sheet1")
    
        Set oShell = CreateObject("Shell.Application")
    
        ' Add fixed path to the folder.
        FolderPath = "C:\Users\Owner\Documents"
    
        ' Let User choose the Folder.
        'Set oFolder = oShell.BrowseForFolder(0&, "Please Select the Folder with the Note Files.", 17)
        'If oFolder Is Nothing Then Exit Sub Else FolderPath = oFolder.Self.Path
    
        Set oFolder = oShell.Namespace(FolderPath)
    
        Set oFiles = oFolder.Items
    
        oFiles.Filter 64, "*.csv;*.txt"
    
        For Each oFile In oFiles
            Open FolderPath & "\" & oFile For Binary Access Read As #1
            ReDim Data(LOF(1))
            Get #1, , Data
            Close #1
    
            Text = StrConv(Data, vbUnicode)
    
            With WS
                NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
                .Range("A" & NextRow) = oFile
                .Range("B" & NextRow) = Text
            End With
        Next oFile
    Last edited by Tinbendr; 09-24-2014 at 03:35 PM.
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    09-24-2014
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2013
    Posts
    2

    Re: Folder of text files into excel sheet

    Thanks for your reply, much appreciated. If I can future out what to do with your code, I'll post back with the result!

  4. #4
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Folder of text files into excel sheet

    Copy the code into a standard module.

    Change path as required to point to text files. While the cursor is inside the code, press F8.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Copy excel and text files from one folder to another folder...
    By annupojupradeep in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-26-2014, 06:34 AM
  2. Copying multiple text files from a folder into excel
    By WinningKing in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-20-2014, 08:47 AM
  3. Replies: 0
    Last Post: 06-17-2013, 05:33 AM
  4. excel macro to copy data from multi text files in a folder to a single work sheet
    By dil_se in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-16-2012, 08:10 AM
  5. Pull out numbers and text from 10+ excel files in a folder
    By ammarkhan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-24-2012, 03:17 PM

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