+ Reply to Thread
Results 1 to 7 of 7

Import Multiple Text Files Into Single Worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    12-22-2009
    Location
    San Francisco
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: Import Multiple Text Files Into Single Worksheet

    Leith,

    Thank you for your in-depth response.

    I notice the title shows up in Column A (excellent!) while the rest of the text shows up in multiple columns (instead of all in cell B1).

    Do you know how to move all the text into once cell only?

    I've attached the workbook with two sheets:

    Sheet #1 is what the macro creates
    Sheet #2 is what we need it to look like

    (Note: I copied the same article twice just for demonstration purposes)

    Again, thank you SO much for your help!

    Best,

    Coz
    Attached Files Attached Files

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Import Multiple Text Files Into Single Worksheet

    Hello Coz,

    Here is the updated macro. The attached workbook has a button on "sheet1" to run the macro.

    Macro Code to Read Text Files
    Sub ReadTextFiles()
    
      Dim CellData As String
      Dim FileName As String
      Dim FilePath As String
      Dim LineCnt As Long
      Dim R As Long
      Dim Text As String
      Dim Wks As Worksheet
      
        R = 1
        Set Wks = Worksheets("Sheet1")
        
        FilePath = "C:\Users\Platinum\Desktop\ExcelMacro"
        
        FileName = Dir(FilePath & "\*.txt")
        
          Do While FileName <> ""
            LineCnt = 0
            CellData = ""
            N = FreeFile
            Open FilePath & "\" & FileName For Input As #N
              Do While Not EOF(N)
                Line Input #N, Text
                LineCnt = LineCnt + 1
                If Text = "" Then Text = " " & vbLf
                If LineCnt = 1 Then
                  Wks.Cells(R, "A").Value = Text
                Else
                   CellData = CellData & Text
                End If
              Loop
              Wks.Cells(R, "B").Value = CellData
            Close #N
            R = R + 1
            FileName = Dir()
          Loop
        
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    12-22-2009
    Location
    San Francisco
    MS-Off Ver
    Excel 2007
    Posts
    31

    [SOLVED]Re: Import Multiple Text Files Into Single Worksheet

    Quote Originally Posted by Leith Ross View Post
    Hello Coz,

    Here is the updated macro. The attached workbook has a button on "sheet1" to run the macro.

    Macro Code to Read Text Files
    Sub ReadTextFiles()
    
      Dim CellData As String
      Dim FileName As String
      Dim FilePath As String
      Dim LineCnt As Long
      Dim R As Long
      Dim Text As String
      Dim Wks As Worksheet
      
        R = 1
        Set Wks = Worksheets("Sheet1")
        
        FilePath = "C:\Users\Platinum\Desktop\ExcelMacro"
        
        FileName = Dir(FilePath & "\*.txt")
        
          Do While FileName <> ""
            LineCnt = 0
            CellData = ""
            N = FreeFile
            Open FilePath & "\" & FileName For Input As #N
              Do While Not EOF(N)
                Line Input #N, Text
                LineCnt = LineCnt + 1
                If Text = "" Then Text = " " & vbLf
                If LineCnt = 1 Then
                  Wks.Cells(R, "A").Value = Text
                Else
                   CellData = CellData & Text
                End If
              Loop
              Wks.Cells(R, "B").Value = CellData
            Close #N
            R = R + 1
            FileName = Dir()
          Loop
        
    End Sub
    Leith, you are a GOLDEN GOD!

    Thank you, thank you, thank you!

    Happy New Year!

    -Coz

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Import Multiple Text Files Into Single Worksheet

    Hello Coz,

    You're welcome and Happy New year!

+ 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