+ Reply to Thread
Results 1 to 10 of 10

Macro to export column header and each row as seperate txt file

  1. #1
    Registered User
    Join Date
    11-12-2012
    Location
    Nashville
    MS-Off Ver
    Excel 2007
    Posts
    8

    Macro to export column header and each row as seperate txt file

    I found a great macro when searching that exports each row to a seperate file perfectly (sorry, didn't get the author to give credit), but I need the column header to be the first line in the text file, then the row of data. Can someone help me with the edit to include that info?

    Sub WriteText()

    Dim firstrow As Integer, rownum As Integer, colnum As Integer
    Dim filename As String
    Dim Filelocation As String
    Dim ws As Worksheet

    Set ws = Worksheets("Sheet1")

    Filelocation = "C:\users\hansenbrad\My Documents\QuestFiles\"

    With ws
    rownum = 2
    While .Cells(rownum, 1) <> ""
    filename = .Cells(rownum, 1).Value
    Open (Filelocation & filename & ".txt") For Output As #1

    colnum = 1
    While .Cells(rownum, colnum) <> 0
    Print #1, ws.Cells(rownum, colnum) & " ";
    colnum = colnum + 1
    Wend
    Close #1
    rownum = rownum + 1
    Wend

    End With

    End Sub
    Last edited by bhansen20; 11-13-2012 at 11:08 AM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to export column header and each row as seperate txt file

    attach a sample file
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    11-12-2012
    Location
    Nashville
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to export column header and each row as seperate txt file

    Of the text file wanted or the original excel?

  4. #4
    Registered User
    Join Date
    11-12-2012
    Location
    Nashville
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to export column header and each row as seperate txt file

    I tried to upload but it failed. Row 1 is the column headers, then I want each row after that to output the way the above macro works. Here is a paste of the sample output:

    sAMAccountName sAMAccountName displayName name userPrincipalName scriptPath msRTCSIP-OriginatorSid
    BNA0DLH JohnDoe Doe, John Doe, John JohnDoe@business.com logon.bat S-1-5-21-799300354-1225732147-196506527-526199

    The spacing doesn't paste properly, but you can get the idea.
    Last edited by bhansen20; 11-13-2012 at 10:54 AM.

  5. #5
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Macro to export column header and each row as seperate txt file

    Try to zip the files. Id upload both.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  6. #6
    Registered User
    Join Date
    11-12-2012
    Location
    Nashville
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to export column header and each row as seperate txt file

    Here is another try.....

    the name of the files should be the second column.txt, I forgot to mention that earlier.
    Attached Files Attached Files

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Macro to export column header and each row as seperate txt file

    Give this a try

    Please Login or Register  to view this content.
    Last edited by mike7952; 11-13-2012 at 12:28 PM.

  8. #8
    Registered User
    Join Date
    11-12-2012
    Location
    Nashville
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to export column header and each row as seperate txt file

    That worked great, thank you!

  9. #9
    Registered User
    Join Date
    11-12-2012
    Location
    Nashville
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to export column header and each row as seperate txt file

    I have found another issue with this macro, I need the resulting text files to not have a hard return at the end of the file. Any ideas?

  10. #10
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551
    What do mean by hard return at the end?

+ 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