+ Reply to Thread
Results 1 to 1 of 1

Import Multiple txt Files

Hybrid View

james_ross Import Multiple txt Files 01-30-2013, 07:16 AM
  1. #1
    Registered User
    Join Date
    01-07-2013
    Location
    Warwickshire, England
    MS-Off Ver
    Excel 2010
    Posts
    7

    Import Multiple txt Files

    Hello,

    I need to import multiple text files into 1 sheet. I already have found a code from another post on how to do this. However I need it to separate the rows and columns. If someone would be able to advise me on how to do this it would be most appreciated

    Here is the code.

    Sub ImportTextFilesMacro_jross()
        
        'Change the folder path as needed
        Const strFolderPath As String = "C:\Users\jross\Desktop\deflector_On"
        
        'Declare variables
        Dim FSO As Object                   'Used to access the Windows FileSystem
        Dim strFileName As String           'Used to store the filename
        Dim arrText(1 To 65000) As String   'Used to store the .txt file's text
        Dim TextIndex As Long               'Used by arrText for storing the text in the correct location
        
        'Access the Windows FileSystem
        Set FSO = CreateObject("Scripting.FileSystemObject")
        
        'Use the folderpath to find all the first .txt file in that folder
        strFileName = Dir(strFolderPath & "\*.txt")
        
        'Loop through all the text files
        Do While Len(strFileName) > 0
            'Found a text file, increase the TextIndex
            TextIndex = TextIndex + 1
            
            'Use the Windows FileSystem to read the text file and store that information in arrText
            arrText(TextIndex) = FSO.OpenTextFile(strFolderPath & "\" & strFileName).ReadAll
            
            strFileName = Dir   'Advance the loop
        Loop
        
        'If at least 1 text file was found, output the results in column C
        If TextIndex > 0 Then Range("C1").Resize(TextIndex).Value = Application.Transpose(arrText)
        
        'Cleanup objects and arrays
        Set FSO = Nothing
        Erase arrText
        
    End Sub
    Thank you
    Last edited by james_ross; 01-30-2013 at 07:20 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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