+ Reply to Thread
Results 1 to 4 of 4

Export rows to delimited txt files

Hybrid View

  1. #1
    Registered User
    Join Date
    12-06-2012
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    3

    Question Export rows to delimited txt files

    Hi all,

    I've run pre-written macros before, but am definitely a novice about VBA. I have a problem that I am confident can be solved with a script but definitely need some help.

    I have a spreadsheet with 1,000+ rows and 50+ columns, each row is a separate record. Here is what I need to do:
    -Export each row as a separate txt file that is tab delimited for the columns
    -Use the cell in the first column as the name of the txt file
    -Preferably include the header row in each txt file (so each txt file would include 2 rows: first txt would have rows 1 and 2, next file rows 1 and 3, etc.); this would mean that the txt file name would need to come from the first cell in the subsequent (non-header) row

    I really just need to be able to do the first two, the third would be the cherry on top.

    Thanks for any help!
    Jess

  2. #2
    Registered User
    Join Date
    01-15-2008
    Posts
    16

    Re: Export rows to delimited txt files

    A little verbose but will get the job done.

    Sub save_rows_as_text_files()
        Dim savePath As String
        Dim strName As String
        
        savePath = "C:\Documents and Settings\"
        Set thisbook = ActiveWorkbook
        
        For i = 2 To ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
            If Range("A" & i) <> "" Then
                thisbook.Activate
                Rows("1:1").Copy
                Set newbook = Workbooks.Add
                newbook.Activate
                ActiveSheet.Paste
                thisbook.Activate
                Rows(i & ":" & i).Copy
                newbook.Activate
                ActiveSheet.Range("A2").Select
                ActiveSheet.Paste
                strName = ActiveSheet.Range("A2").Value
                ActiveWorkbook.SaveAs Filename:=savePath & strName, FileFormat:=xlText, CreateBackup:=False
                newbook.Close False
            End If
        Next
    End Sub

  3. #3
    Registered User
    Join Date
    12-06-2012
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Export rows to delimited txt files

    Wow! Thanks CriticalBill! This is exactly what I needed!

    Thanks again,
    Jess

  4. #4
    Registered User
    Join Date
    12-06-2012
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Export rows to delimited txt files

    Wow! Thanks CriticalBill! This is exactly what I needed!

    Thanks again,
    Jess

+ 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