+ Reply to Thread
Results 1 to 3 of 3

Splitting txt file into multiple txt files

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-28-2010
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    105

    Splitting txt file into multiple txt files

    Hello,

    I would like to create a macros that can split a txt file (opened as a tab delimited xls file like the attached ) into seperate txt files in the current directory. I would like each txt file to be named after its article #. I would like the break to come right after Journal-Code:

    i.e. so this would be named "1.txt"

    
                                   1 of 159 DOCUMENTS
    
    
    
                                      TendersInfo
    
                               January 6, 2010 Wednesday
    
    India : News Roundup: Novatium To Raise $10M Growth Fund
    
    BYLINE: datta03
    
    LENGTH: 410 words
    
    
    ***Novatium believes the fund will help it market its products internationally,
    besides reaching every corner of India.
    Dorf Ketal Buys DuPont Unit For $40M - Mumbai-based specialty chemicals company
    Dorf Ketal Chemicals has acquired the global specialty catalysts business of
    DuPont Chemicals and Fluoroproducts for around $40 million. The acquired unit....
    
    .............UMINOUS COAL UNDERGROUND MINING (53%); NAICS521110
    MONETARY AUTHORITIES - CENTRAL BANK (50%); SIC6011 FEDERAL RESERVE BANKS (50%)
    
    GEOGRAPHIC: MUMBAI, INDIA (91%) TAMIL NADU, INDIA (58%) INDIA (94%)
    
    LOAD-DATE: January 13, 2010
    
    LANGUAGE: ENGLISH
    
    PUBLICATION-TYPE: Web Publication
    
    JOURNAL-CODE: 81
    
    

    The following is the second txt file named "2":

                 Copyright 2010 TendersInfo - Euclid Infotech Pvt. Ltd.
                                  All Rights Reserved
                                 Provided by Al Bawaba
    
    
                                   2 of 159 DOCUMENTS
    
    
    
                                      TendersInfo
    
                                January 19, 2010 Tuesday
    
    United States : DuPont solar project to mean up to 80 new jobs in Circleville
    area
    
    BYLINE: deepak03
    
    LENGTH: 189 words
    
    
    ***DuPont will invest $175 million in its Pickaway County plant and hire up to
    80 new workers to build solar-power components in there, the company said today.
    
    .............................
    
    PERSON: TED STRICKLAND (92%); STEVE AUSTRIA (53%)
    
    GEOGRAPHIC: OHIO, USA (79%) UNITED STATES (92%)
    
    LOAD-DATE: January 20, 2010
    
    LANGUAGE: ENGLISH
    
    PUBLICATION-TYPE: Web Publication
    
    JOURNAL-CODE: 81
    and so on..
    The original aggregated txt file always starts with 1, and goes in sequential order to N, there are no omissions.
    Attached Files Attached Files
    Last edited by undergraduate; 05-02-2010 at 10:42 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Splitting txt file into multiple txt files

    Maybe like this. JOURNAL-CODE is missing in several instances.
    Option Explicit
    
    Sub ExportTextByDoc()
        Dim wks         As Worksheet
        Dim r           As Range
        Dim iRow        As Long
        Dim lRow        As Long
        Dim iRowBeg     As Long
        Dim iRowEnd     As Long
        Dim sFile       As String
        Dim sPath       As String
        Dim iFF         As Integer
    
        sPath = ThisWorkbook.Path & "\"
    
        Set wks = ActiveSheet
        With wks
            lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            Set r = .Range("A1", Cells(lRow, "A"))
        End With
    
        iRow = 1
    
        Do
            Do Until r(iRow).Value Like "*of*DOCUMENTS"
                iRow = iRow + 1
                If iRow > lRow Then Exit Sub
            Loop
    
            sFile = Split(WorksheetFunction.Trim(r(iRow).Value), " ")(0) & ".txt"
            If Len(sFile) < 7 Then sFile = Right("00" & sFile, 7)
            iRowBeg = iRow
            iRowEnd = r.Find(What:="JOURNAL-CODE", _
                             After:=r(iRow), _
                             SearchDirection:=xlNext, _
                             LookAt:=xlPart).Row
            If iRowEnd < iRowBeg Then Exit Sub
    
            iFF = FreeFile
            Open sPath & sFile For Output As iFF
            Write #iFF, CatRng(wks.Range(r(iRowBeg), r(iRowEnd)))
            Close #iFF
            
            iRow = iRowEnd + 1
        Loop
    End Sub
    
    Function CatRng(r As Range, _
                    Optional sColSep As String = vbTab, _
                    Optional sRowSep As String = vbCrLf) As String
        ' copy similar to the way Excel copies
        Dim iRow        As Long
        Dim iCol        As Long
        Dim sRow        As String
    
        For iRow = 1 To r.Rows.Count
            For iCol = 1 To r.Columns.Count
                sRow = sRow & sColSep & r(iRow, iCol).Text
            Next iCol
            CatRng = CatRng & Mid(sRow, Len(sColSep) + 1) & sRowSep
            sRow = ""
        Next iRow
    End Function
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    01-28-2010
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    105

    Re: Splitting txt file into multiple txt files

    thank you, that worked very well. I replaced Journal Code with Language:English, since that appeared in all the articles at the end. Thanks as always shg its really appreciated.

+ 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