+ Reply to Thread
Results 1 to 13 of 13

Split data in one worksheets into multiple sheets?

Hybrid View

  1. #1
    Registered User
    Join Date
    08-08-2012
    Location
    North Haven, CT
    MS-Off Ver
    Excel 2007
    Posts
    7

    Split data in one worksheets into multiple sheets?

    Hello - can someone help me with a macro that would separate the data in "All" into separate sheets by Supervisor? I have attached a test with an example. Thank you so much!!
    Attached Files Attached Files

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

    Re: Split data in one worksheets into multiple sheets?

    Try this code
    Option Explicit
    
    Sub split_data()
    Dim i As Long, lrow As Long
    Dim sname As String
    
    Application.ScreenUpdating = False
    
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name <> "All" Then
            lrow = Worksheets(i).Range("A" & Rows.Count).End(xlUp).Row
            If lrow > 1 Then Worksheets(i).Range("A2:O" & lrow).Delete
        End If
    Next i
    
    With Worksheets("All")
        lrow = .Range("B" & .Rows.Count).End(xlUp).Row
        For i = 2 To lrow
            sname = .Range("C" & i).Value
            .Range("B" & i & ":P" & i).Copy Worksheets(sname).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
        Next i
    End With
    
    Application.ScreenUpdating = True
    
    End Sub
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose View | Macros
    Select a macro in the list, and click the Run button

    Let me know if you need a small addition to the code which checks if the sheetname exists and adds one if it doesnt.
    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]

  3. #3
    Registered User
    Join Date
    08-08-2012
    Location
    North Haven, CT
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Split data in one worksheets into multiple sheets?

    Thanks, that would be great! Also, I noticed that when I deleted someone from "all" it wasn't deleted on the Sup sheet. I guess I would just rerun the macro when I delete someone?

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

    Re: Split data in one worksheets into multiple sheets?

    Yes, if you delete / add someone, you need to rerun the code. It will clear all the sup sheets and re-populate.

    The updated code is here -
    Option Explicit
    
    Sub split_data()
    Dim i As Long, lrow As Long
    Dim sname As String
    
    Application.ScreenUpdating = False
    
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name <> "All" Then
            lrow = Worksheets(i).Range("A" & Rows.Count).End(xlUp).Row
            If lrow > 1 Then Worksheets(i).Range("A2:O" & lrow).Delete
        End If
    Next i
    
    With Worksheets("All")
        lrow = .Range("B" & .Rows.Count).End(xlUp).Row
        For i = 2 To lrow
            sname = .Range("C" & i).Value
            If Not Evaluate("ISREF('" & sname & "'!A1)") Then
                Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sname
                .Range("B1:P1").Copy Worksheets(sname).Range("A1")
            End If
            .Range("B" & i & ":P" & i).Copy Worksheets(sname).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
            worksheets(sname).cells.entirecolumn.autofit
        Next i
    End With
    
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by arlu1201; 08-14-2012 at 07:51 AM.

  5. #5
    Registered User
    Join Date
    08-08-2012
    Location
    North Haven, CT
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Split data in one worksheets into multiple sheets?

    When I try in the real workbook (which has actual supervisor names rather than "Sup 1"), I am getting "variable not defined" - am I missing a step?

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

    Re: Split data in one worksheets into multiple sheets?

    I have edited the code above. I had some extra text in there by mistake.

    Please try and let me know.

  7. #7
    Registered User
    Join Date
    08-08-2012
    Location
    North Haven, CT
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Split data in one worksheets into multiple sheets?

    Now I'm getting subscript out of range...

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

    Re: Split data in one worksheets into multiple sheets?

    Which line is highlighted in yellow when you get the error?

  9. #9
    Registered User
    Join Date
    08-08-2012
    Location
    North Haven, CT
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Split data in one worksheets into multiple sheets?

    With Worksheets("All")

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

    Re: Split data in one worksheets into multiple sheets?

    Is your main worksheet named "All" as it was in the summary file? When you get a subscript out of range error, its all got to do with the sheetname.

  11. #11
    Registered User
    Join Date
    08-08-2012
    Location
    North Haven, CT
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Split data in one worksheets into multiple sheets?

    Now I feel dumb - I should have figured that out! It works beautifully now! Thank you for your patience and assistance!!

  12. #12
    Registered User
    Join Date
    08-08-2012
    Location
    North Haven, CT
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Split data in one worksheets into multiple sheets?

    When I rerun the macro it's not replacing the sheets - I have to delete the sheets and then rerun the macro. Fixable?

    ---------- Post added at 10:58 AM ---------- Previous post was at 10:53 AM ----------

    And while I'm asking, is there a way for the individual sheets to maintain the formatting (column width etc) of the "all" sheet? Thanks!

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

    Re: Split data in one worksheets into multiple sheets?

    The macro will clear the contents of the existing sheets and re-populate with data.

    If the sheet name does not exist, it will create it for you.

    I have added the code line to autofit the columns of each sheet.

+ 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