+ Reply to Thread
Results 1 to 7 of 7

Thread: Export each row as text file using first cell in row as title

  1. #1
    Registered User
    Join Date
    09-14-2011
    Location
    Grand Rapids, MI
    MS-Off Ver
    Excel 2003
    Posts
    4

    Export each row as text file using first cell in row as title

    Hello, Excel Forum!

    I posted a question in an old forum and was kindly instructed that I was in violation of the forum rules, so I am now creating a new thread with a more concise question.

    I have a .CSV file with two text columns; one serving as a title and the other as a body paragraph. For example:

    _A_________ B__________
    1 Title______ Content__________
    2 Red______ This is a sentence...
    3 Orange____And another sentence...
    4 Yellow____ Yet another...
    5 Green_____You get the idea...

    I am hoping there is a way to save each of the rows in the worksheet as a separate document, preferably a .txt file but a Word .doc or .rtf would be fine, and use the text from the first column to title the file and the text from the second column to populate the content of the document. I'm not very familiar with VisualBasic or macros but would be willing to learn if pointed in the right direction. Any help is greatly appreciated.

    Thanks

    -Aaron
    Last edited by aaronmb; 10-06-2011 at 12:30 PM. Reason: Solved with a macro

  2. #2
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    703

    Re: Export each row as text file using first cell in row as title

    Hi,

    you could do something like this

    This code loops threw your usedrange on sheet 1 and writes a text file using cell in column a as save title and column b as content.

    
    For each cell in Sheets(1).range("A1:A" & Sheets(1).UsedRange.Rows.Count)
            saveText = cell.text
    
              open "E:\yourfolder\" &  saveText & ".txt" for output as #1
              Print #1,cell.offset(1,0).Text
              Close #1
    next cell
    Steffen Thomsen

  3. #3
    Registered User
    Join Date
    09-14-2011
    Location
    Grand Rapids, MI
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Export each row as text file using first cell in row as title

    Hi Stephen

    Thank you for your quick response. I gave the macro a try but there seems to be a missing step somewhere. Everything works as it should except I think the offset command is looking to the next cell down in the column instead of the adjacent cell in the row. I think I can make it work by simply moving all of the information into a single column with alternating title/content rows but if you know of a way to have the offset occur horizontally instead of vertically I would appreciate your expert advice.

    Thanks again

    -aaron

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

    Re: Export each row as text file using first cell in row as title

     
    Sub snb()
     sn= Sheets(1).usedrange
     
     for j=1 to ubound(sn)
      open "E:\yourfolder\" &  sn(j,1) & ".txt" for output as #1
       Print #1, sn(j,2)
      Close #1
     next
    End Sub



  5. #5
    Registered User
    Join Date
    09-14-2011
    Location
    Grand Rapids, MI
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Export each row as text file using first cell in row as title

    That did it! Many thanks. One of these days I'll have to sit down with a book and figure out exactly what all that programming language means should I ever want to modify it. If you recommend any guides to learning VBA-code I wouldn't mind taking a look at one.

    -Aaron

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

    Re: Export each row as text file using first cell in row as title

    Books by Guy Hart Davis, John Walkenbach e.g.



  7. #7
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    703

    Re: Export each row as text file using first cell in row as title

    Hi,

    can see you got the answer needed.

    Was in a bit of a hurry when i wrote it.

    This line
    cell.offset(1,0).Text
    Should be changed to
    cell.offset(0,1).Text
    Steffen Thomsen

+ 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