Closed Thread
Results 1 to 7 of 7

Splitting a file into multiple files as per a column

Hybrid View

  1. #1
    Registered User
    Join Date
    09-02-2010
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    1

    Splitting a file into multiple files as per a column

    Hi,

    is there anyway of making a macro that when run it splits the main workbook into several workbooks as per a column in the main workbook.

    eg:

    Column A has only 10 differing entries. Each of the 250 lines of the workbook has one of those 10 values in column A. I am trying to split the file into 10 seperate files for each one of those values. sorry, its quite hard to explain.

    imagine my column A is: (+ there is data all the way up to column AD)

    a
    b
    b
    a
    c
    a
    b

    i want to create a macro that will split this into a file of all the a's, a file of all the b's and a file of all the c's

    Now i could record a macro where i do this manually but was wondering if theres a quicker way?

    Thank you in advance

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Splitting a file into multiple files as per a column

    Like this? Assumes you have headers in row 1, and you need to change the path.
    Sub x()
     
    Dim r As Long, rng As Range, ws As Worksheet
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    With Sheets("Sheet1")
        Sheets.Add().Name = "temp"
        .Range("A1", .Range("A" & Rows.Count).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("temp").Range("A1"), Unique:=True
         For Each rng In Sheets("temp").Range("A2", Sheets("temp").Range("A2").End(xlDown))
            .AutoFilterMode = False
            .Range("A1").AutoFilter field:=1, Criteria1:=rng
            Set ws = Sheets.Add
            .AutoFilter.Range.Copy ws.Range("A1")
            ws.Name = rng
            ws.Move
            ActiveWorkbook.Close SaveChanges:=True, Filename:="C:\" & rng & ".xls"
        Next rng
        .AutoFilterMode = False
        Sheets("temp").Delete
    End With
         
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
    End Sub

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Splitting a file into multiple files as per a column

    Sub tst()
      With sheet1.UsedRange.Columns(1)
        sq = .Value
        
        For j = 1 To UBound(sq)
          If InStr(c01 & "|", "|" & sq(j, 1) & "|") = 0 Then
            c01 = c01 & "|" & sq(j, 1)
            .Replace sq(j, 1), "", xlWhole
            .SpecialCells(4).EntireRow.Copy Sheet.Cells(1, 1)
            With Sheet2
              .Cells(1, 2).CurrentRegion.Resize(, 1).Offset(, -1) = sq(j, 1)
              .Copy
              .Cells.ClearContents
            End With
            With ActiveWorkbook
              .SaveAs sq(j, 1) & ".xls"
              .Close False
            End With
            .Value = sq
          End If
        Next
      End With
    End Sub

  4. #4
    Registered User
    Join Date
    07-15-2011
    Location
    Yes
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Splitting a file into multiple files as per a column

    Quote Originally Posted by StephenR View Post
    Like this? Assumes you have headers in row 1, and you need to change the path.
    Sub x()
     
    Dim r As Long, rng As Range, ws As Worksheet
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    With Sheets("Sheet1")
        Sheets.Add().Name = "temp"
        .Range("A1", .Range("A" & Rows.Count).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("temp").Range("A1"), Unique:=True
         For Each rng In Sheets("temp").Range("A2", Sheets("temp").Range("A2").End(xlDown))
            .AutoFilterMode = False
            .Range("A1").AutoFilter field:=1, Criteria1:=rng
            Set ws = Sheets.Add
            .AutoFilter.Range.Copy ws.Range("A1")
            ws.Name = rng
            ws.Move
            ActiveWorkbook.Close SaveChanges:=True, Filename:="C:\" & rng & ".xls"
        Next rng
        .AutoFilterMode = False
        Sheets("temp").Delete
    End With
         
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
    End Sub


    Great reply! Worked perfect -Thank you!

  5. #5
    Registered User
    Join Date
    05-31-2012
    Location
    new brunswick
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Splitting a file into multiple files as per a column

    Hi there- I have a similar problem as DCS2010 and I am not very good at VB. I got a macro to work called breakout but it split the data into multiple worksheets within the same workbook. I am looking for a macro that will split a worksheet into multiple workbooks or files based on a column. The column has about 50 customer names, so at each change in customer name, I would like a new workbook created and saved in the same folder as the original file. I would also like the name of the new workbook to be a combination of the header row for that column and the name in the column. Here is an example of the header row and data:

    Example IF Header Row is: 2012 P5 Time for and first company name is Tim Hortons then the File name should be 2012 P5 Time for Tim Hortons.xls
    Can anyone help with this one? Thanks!
    Last edited by kbustin; 06-01-2012 at 08:33 AM.

  6. #6
    Registered User
    Join Date
    05-31-2012
    Location
    new brunswick
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Splitting a file into multiple files as per a column

    Hi- Sorry forgot to mention I use Excel 2010 - thanks!

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Splitting a file into multiple files as per a column

    Kbustin,

    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

Closed 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