+ Reply to Thread
Results 1 to 5 of 5

seperate duplicated entries into multiple worksheets/files

Hybrid View

  1. #1
    Registered User
    Join Date
    05-07-2007
    Posts
    3

    seperate duplicated entries into multiple worksheets/files

    I am a novice excel user. any help would be appreciated.

    I have a dozen of files. They contains thousands of records. The fourth column of each file is the 'name' column. Each entry of the name has the same number of duplicates (3~6 depending on the file) in that column. I need to separate the rows with those names into deferent files or worksheets for subsequence analysis. For example: first rows of 'record 1', 'record 2', ..., will be exported to one file/worksheet; second appearance of rows with 'record 1', 'record 2', ..., will be exported into the another file/worksheet; and so on.

    I don't want to do it manually, there are too many records.

    Can it be done?

    Thanks a lot!
    Last edited by liux; 05-07-2007 at 11:30 AM.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    This look achievable by macro - can you provide an example file?
    Martin

  3. #3
    Registered User
    Join Date
    05-07-2007
    Posts
    3
    Here is a small part of the big file. (the order of rows is not identical to the original file)

    it contains 4 set of 6 unique ID (the original file contains 4 sets of 2000 ID). It needs to be separated into 4 files, each contains one set of ID.

    Is it hard to do so?

    Thanks!
    Attached Files Attached Files

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    This code should help. Paste it into a module sheet in the VBA editor (Alt F11)

    You will need to list the files containing your data including full path in column A starting in the first row. The output files will be created in the same folder as the input files.

    Sub SplitData()
    Dim FileLine As String
    Dim ValueArray()
    Close
    FileNumber = FreeFile()
    FileNumber1 = FileNumber + 1
    FileNumber2 = FileNumber + 2
    FileNumber3 = FileNumber + 3
    FileNumber4 = FileNumber + 4
    FileNumber5 = FileNumber + 5
    FileNumber6 = FileNumber + 6
    
    For N = 1 To Cells(65536, 1).End(xlUp).Row
        Erase ValueArray
        ReDim ValueArray(2, 0)
        Open Cells(N, 1) For Input As #FileNumber
        Open Left(Cells(N, 1), Len(Cells(N, 1)) - 4) & " 1.txt" For Output As #FileNumber1
        Open Left(Cells(N, 1), Len(Cells(N, 1)) - 4) & " 2.txt" For Output As #FileNumber2
        Open Left(Cells(N, 1), Len(Cells(N, 1)) - 4) & " 3.txt" For Output As #FileNumber3
        Open Left(Cells(N, 1), Len(Cells(N, 1)) - 4) & " 4.txt" For Output As #FileNumber4
        Open Left(Cells(N, 1), Len(Cells(N, 1)) - 4) & " 5.txt" For Output As #FileNumber5
        Open Left(Cells(N, 1), Len(Cells(N, 1)) - 4) & " 6.txt" For Output As #FileNumber6
            
        Do While Not EOF(FileNumber)
            Line Input #FileNumber, FileLine
            LineArray = Split(FileLine, Chr$(9))
            If LineArray(3) <> "ID" Then
                NewID = True
                For M = 1 To UBound(ValueArray, 2)
                    If LineArray(3) = ValueArray(1, M) Then
                        NewID = False
                        ValueArray(2, M) = ValueArray(2, M) + 1
                        TargetFile = ValueArray(2, M)
                    End If
                Next M
                If NewID = True Then
                    ReDim Preserve ValueArray(2, UBound(ValueArray, 2) + 1)
                    ValueArray(1, UBound(ValueArray, 2)) = LineArray(3)
                    ValueArray(2, UBound(ValueArray, 2)) = 1
                    TargetFile = 1
                End If
                Select Case TargetFile
                    Case Is = 1
                        Print #FileNumber1, FileLine
                    Case Is = 2
                        Print #FileNumber2, FileLine
                    Case Is = 3
                        Print #FileNumber3, FileLine
                    Case Is = 4
                        Print #FileNumber4, FileLine
                    Case Is = 5
                        Print #FileNumber5, FileLine
                    Case Is = 6
                        Print #FileNumber6, FileLine
                End Select
            Else
                Print #FileNumber1, FileLine
                Print #FileNumber2, FileLine
                Print #FileNumber3, FileLine
                Print #FileNumber4, FileLine
                Print #FileNumber5, FileLine
                Print #FileNumber6, FileLine
            End If
        Loop
        Close #FileNumber
        Close #FileNumber1
        Close #FileNumber2
        Close #FileNumber3
        Close #FileNumber4
        Close #FileNumber5
        Close #FileNumber6
    Next N
    End Sub
    Hope this helps.

  5. #5
    Registered User
    Join Date
    05-07-2007
    Posts
    3
    thank you!

+ 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