+ Reply to Thread
Results 1 to 8 of 8

Thread: Help exporting columns into multiple text files

  1. #1
    Registered User
    Join Date
    06-02-2011
    Location
    portland, or
    MS-Off Ver
    Excel 2003
    Posts
    4

    Help exporting columns into multiple text files

    Hello,

    I currently have an excel file with 1000+ columns and would like to export each individual one into a .txt file. The first row of each column has a different name, which I would also like to be the name of the .txt file created. I've tried using macros that I found browsing this site, but have been unsuccessful thus far. I attached a excel file I'm working with (smaller version) for a clearer picture.

    Thanks for the help/insight!!
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    PA
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    926

    Re: Help exporting columns into multiple text files

    Hi,

    I found and edit this script below, see if this does what you need. Leith Ross was the person who made the orginal script. But this should get you going in the right direction with some of my edits. Post back any issue's you may have.

    Sub CreateTextFile()
      Const ForAppending As Long = 8
      Const DefaultFormat As Long = -2
      Const UnicodeFormat As Long = -1
      Const AsciiFormat As Long = 0
        Dim C As Long, R As Long
        Dim FileName As String
        Dim Filepath As String
        Dim fso As Object
        Dim LastCol As Long
        Dim LastRow As Long
        Dim Rng As Range
        Dim StartCol As Long
        Dim StartRow As Long
        Dim TxtData As String
        Dim TxtFile As Object
        Dim MyName As String
        Dim MyRow As Long
        Dim MyCol As Long
     
     
     
          StartRow = 1
          StartCol = 1
          MyRow = 1
          MyCol = 1
     
            LastCol = Cells(StartRow, Columns.Count).End(xlToLeft).Column
            LastRow = Cells(Rows.Count, StartCol).End(xlUp).Row
     
     
            For C = StartCol To LastCol
     
                MyName = Cells(MyRow, MyCol).Value
                Set fso = CreateObject("Scripting.FileSystemObject")
                Set TxtFile = fso.OpenTextFile("c:\Test\" & MyName & ".txt", ForAppending, DefaultFormat)
              For R = StartRow + 1 To LastRow
     
     
                Set Rng = Range(Cells(StartRow, C), Cells(LastRow, C))
                  If WorksheetFunction.CountIf(Rng, "<>*") <> 0 Then
     
                      TxtData = TxtData & Cells(R, C).Value & vbTab
     
                  End If
              Next R
     
                    TxtFile.WriteLine (TxtData)
                    TxtData = ""
                    MyCol = MyCol + 1
                    TxtFile.Close
                    Set fso = Nothing
                    Set TxtFile = Nothing
            Next C
     
    End Sub
    .
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

  3. #3
    Registered User
    Join Date
    06-02-2011
    Location
    portland, or
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Help exporting columns into multiple text files

    Thanks for the reply realniceguy5000. Everything looks good except the resulting data in the text files is displayed a little different then I would prefer. Is there a way to arrange the data vertically like this:

    0
    0
    2
    0
    19
    0
    0
    0

    Instead of the current 4 row layout as seen below?

    0 5 0 0 0
    0 0 0 0 0
    3 0 0 2 0
    0 2 0 0 4

    Everything else looks great. Thanks again for all the help.

  4. #4
    Registered User
    Join Date
    06-02-2011
    Location
    portland, or
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Help exporting columns into multiple text files

    Spoke too soon, I just noticed something else. The first 3 columns didn't get processed correctly. It outputs one file with the name of the first 3 columns combined. This is also the same for the text inside of the file.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Help exporting columns into multiple text files

    Hello vund0,

    This macro will create a file for column on the worksheet using the cells in row 1 as the file name. You will need to change the file path to where your files will be stored.
    Sub ExportDataToFiles()
    
      Dim Col As Range
      Dim Data As Variant
      Dim FileName As String
      Dim FilePath As String
      Dim FSO As Object
      Dim Item As Variant
      Dim TextFile As Object
      Dim Wks As Worksheet
      
        FilePath = "C:\Test Folder\"
        
        Set Wks = Worksheets("pplacer_resampled1021")
        Set Rng = Wks.UsedRange
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
        
          For Each Col In Rng.Columns
            ReDim Data(1 To Col.Rows.Count, 1 To 1)
            FileName = FilePath & Col.Cells(1, 1)
            Data = Col.Offset(1, 0).Resize(Col.Rows.Count - 1, 1).Value
              Set TextFile = FSO.OpenTextFile(FileName, 2, True, False)
                For Each Item In Data
                  TextFile.WriteLine Item
                Next Item
              TextFile.Close
          Next Col
          
    End Sub
    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!)

  6. #6
    Registered User
    Join Date
    06-02-2011
    Location
    portland, or
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Help exporting columns into multiple text files

    Thanks Leith Ross. You are a gentleman and a scholar.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Help exporting columns into multiple text files

    Hello vund0,

    You're welcome. Glad to help.
    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!)

  8. #8
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Help exporting columns into multiple text files

    or

    Sub snb()
      sq = Sheets(1).Cells(1).CurrentRegion
      For j = 1 To ubound(sq,2)
        Open "E:\OF\" & sq(1, j) & ".txt" For Output As #1
          Print #1, Join(Application.Transpose(Application.Index(sq, , j)), vbCrLf)
        Close
      Next
    End Sub
    If you go for speed: no need to load an extra library, nor to produce a new instance of an object.
    Open, print & Close are VBA-commands, that are resident in memory.
    The code has only 1 reading instruction.
    The writing instructions are also diminished to a minimum (1 per column).
    Last edited by snb; 06-02-2011 at 05:33 PM.



+ 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.2.0