+ Reply to Thread
Results 1 to 7 of 7

Parsing out file into multiple new files

Hybrid View

  1. #1
    Registered User
    Join Date
    08-24-2013
    Location
    Morgan Hill, CA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Parsing out file into multiple new files

    I need to do this same thing (parse one file into multiple files). I copied the first macro and tried to use it in with my file, but get an error on -- Columns("C:C").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AA1"), Unique:=True -- the error is "The extract range has a missing or illegal field name." Meaningful error to you?? Any idea how I can solve this? Thank you.


    Moderator's Note: Moved from this thread, as per forum rules against taking over other people's threads.
    Last edited by ktalamantez; 08-24-2013 at 08:59 PM. Reason: Moved to its own thread as per hijacking rules. Please read the Forum Rules in the menu bar above. Thanks.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Parsing out file into multiple new files

    1) Move the DATA you already have in column AA over to column W, or some other empty column. The macro is using column AA to create the unique list of Directors.

    2) In case you run the macro multiple times, probably a good idea to erase the named ranges the Advanced Filter creates.

    3) I've updated the code to no longer "select" sheets, unneeded.

    Option Explicit
    
    Sub ParseStudents()
    'JBeaucaire  (11/11/2009)
    'Based on column C, data is filtered to individual sheets
    'Creates sheets and sorts alphabetically in workbook
    Dim LR As Long, i As Long, MyArr
    Dim MyCount As Long, ws As Worksheet
    Application.ScreenUpdating = False
    
    Set ws = Sheets("Original")
    
    With ws
        On Error Resume Next
        .Range("Criteria").Name.Delete
        .Range("Extract").Name.Delete
        On Error GoTo 0
        .Columns("E:E").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("AA1"), Unique:=True
        .Columns("AA:AA").Sort Key1:=.Range("AA2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
        MyArr = Application.WorksheetFunction.Transpose(.Range("AA2:AA" & Rows.Count).SpecialCells(xlCellTypeConstants))
        
        .Range("AA:AA").Clear
        .Range("A1:K1").AutoFilter
        
        For i = 1 To UBound(MyArr)
            .Range("A1:K1").AutoFilter Field:=5, Criteria1:=MyArr(i)
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
            If LR > 1 Then
                If Not Evaluate("=ISREF('" & MyArr(i) & "'!A1)") Then
                    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = MyArr(i)
                Else
                    Sheets(MyArr(i)).Move After:=Sheets(Sheets.Count)
                    Sheets(MyArr(i)).Cells.Clear
                End If
                .Range("A1:K" & LR).Copy Sheets(MyArr(i)).Range("A1")
                .Range("A1:K1").AutoFilter Field:=1
                MyCount = MyCount + Sheets(MyArr(i)).Range("A" & Rows.Count).End(xlUp).Row - 1
                Sheets(MyArr(i)).Columns.AutoFit
            End If
        Next i
        .AutoFilterMode = False
        LR = .Range("A" & .Rows.Count).End(xlUp).Row - 1
        .Activate
    End With
    
    MsgBox "Rows with data: " & LR & vbLf & "Rows copied to other sheets: " & MyCount & vbLf & "Hope they match!!"
    Application.ScreenUpdating = True
    End Sub
    Last edited by JBeaucaire; 08-25-2013 at 02:17 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Parsing out file into multiple new files

    shorter, faster, Col AA doesn't matter
    Sub scode()
    Const cl& = 5
    Dim a As Variant, q As Variant
    Dim rws&, cls&, p&, i&, b As Boolean
    Application.ScreenUpdating = False
    With Sheets.Add(after:=Sheets("Original"))
        Sheets("Original").Cells(1).CurrentRegion.Copy .Cells(1)
        Set a = .Cells(1).CurrentRegion
        rws = a.Rows.Count
        cls = a.Columns.Count
        a.Sort a(1, cl), 2, Header:=xlYes
        .Name = a(2, cl)
        a = a.Resize(rws + 1)
        p = 2
        For i = p To rws + 1
            If a(i, cl) <> a(p, cl) Then
                If b Then
                    Sheets.Add.Name = a(p, cl)
                    .Cells(p, 1).Resize(i - p, cls).Cut Cells(2, 1)
                    Sheets("Original").Cells(1).Resize(, cls).Copy Cells(1)
                End If
                b = True
                p = i
            End If
        Next i
    End With
    Application.ScreenUpdating = True
    End Sub

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Parsing out file into multiple new files

    @Kalak - no disrespect, your code is shorter because there is no error checking. And faster? On a data set with 5000 rows, for instance, your code has to loop 5000 times, while the approach I always suggest only loops the number of unique values in the criteria column. So if only 8 names, then that's 8 loops and done regardless how long that data is, even tens of thousands of rows.

    Anyway, your code looks good, add some error checking so it can be run twice in succession on the same data and that would be ready for prime-time.

    Cheers.

  5. #5
    Registered User
    Join Date
    02-22-2013
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    49

    Re: Parsing out file into multiple new files

    hi,
    am also looking the same solution, one file to multiple files.
    if you got any good result, please give to me also...

    thanks
    Josh

  6. #6
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Parsing out file into multiple new files

    @Jerry B
    Thanks for the considered response.
    Please read the last part of these comments, even if not the rest.

    1. Errors. Depends what errors are to be checked. In this type of problem the likely one is trying to re-create an already-existing worksheet. What happens in such case depends on the user, e.g. replace existing worksheet, or append data at the bottom, or leave the existing sheet as it is, or create a new sheet with an easily-identifiable similar name, or ...
    Allowing for any of these is easily included in the code I posted with little increase in length. But I'd need to know which is wanted. Do you know which one is wanted? So what does your error checking actually check?
    Easy and short enough to give a message box saying "Sheet already exists" or similar, but since the VBA tells you this anyway, what additional purpose is there in including it? It's errors that don't give error messages, such as in your own code, that concern me a lot more. See below.


    2. Loops can be either explicit, or implicit as with the filter type approaches. Whichever way, all rows for this type of problem need be checked, so seems should come down to which approach is more efficient.
    I used shorter and faster as efficiency criteria. Do you prefer others?

    Faster. I wouldn't make such claim without checking it first.
    So, generated a bit of test data with the code below. You can easily check anything I say here with this, or your own test data.

    With the 5000 rows and 8 unique names you refer to, my code took 0.14 secs on an average spec notebook. Your code took just about twice as long.

    Probably not much in that for the average problem.
    But, with 100,000 rows and 8 names, my code took 1.4 secs. Yours took quite a bit longer, but also failed to give the correct result, failing without any error message. Try it and see.

    Since you raised the topic of error checking, and wishing you no disrespect, wouldn't you regard it as reasonable to check that your own codes perform without error, or at least give some error message when they do go wrong?
    So that a user might at least have some reasonable indication, preferably in advance of using it, as to when the code will work and when it won't.
    Sub testdata()
    Sheets("Original").Activate
    ActiveSheet.UsedRange.ClearContents
    Const n As Long = 10^5  'also tried with 5000
    With Cells(1).Resize(n, 10)
        .Cells = "=char(int(rand()*26)+65)&randbetween(1,9)"
        .Resize(, 1).Offset(, 4) = "=rept(char(int(rand()*8)+65),4)"
        .Resize(1) = "=""Hdr"" & column()"
        .Value = .Value
    End With
    Cells(5) = "NAME"
    End Sub

  7. #7
    Registered User
    Join Date
    02-22-2013
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    49

    Re: Parsing out file into multiple new files

    Hi, sorry you carry on.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Import multiple jpg files in one file with file name in next cell
    By samyell in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-15-2013, 07:58 AM
  2. Consolidate Data from Multiple Files (Different File Names) into a Master File
    By dspraveen_23 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-08-2012, 12:59 PM
  3. Replies: 0
    Last Post: 11-27-2012, 01:43 PM
  4. Parsing out file into multiple new files
    By CrystalNewb in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-17-2009, 02:34 PM
  5. Parsing Huge Files of Text
    By ajocius in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-06-2005, 08:45 PM

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