+ Reply to Thread
Results 1 to 6 of 6

Split worksheet into multiple worksheets

Hybrid View

ashtanga Split worksheet into multiple... 08-12-2009, 01:05 PM
JBeaucaire Re: Split worksheet into... 08-12-2009, 03:00 PM
ashtanga Re: Split worksheet into... 08-12-2009, 03:37 PM
ashtanga Re: Split worksheet into... 08-12-2009, 03:40 PM
ashtanga Re: Split worksheet into... 08-12-2009, 03:45 PM
JBeaucaire Re: Split worksheet into... 08-12-2009, 04:43 PM
  1. #1
    Registered User
    Join Date
    08-12-2009
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Smile Split worksheet into multiple worksheets

    This code splits a worksheet into multiple sheets (based on sales person in column A). But when it does it, it deletes the sheets and then re-creates them. However this messes with my formulas I have linked to the split sheets and turns them into #REF! errors.

    Can someone edit the code below to simply clear the cells and replace with the updated data? I will make sure blank sales person worksheets are set up in advance.

    Thanks!

    Sub Split_Worksheets()
    Dim rRange As Range, rCell As Range
    Dim wSheet As Worksheet
    Dim wSheetStart As Worksheet
    Dim strText As String
    
     Set wSheetStart = ActiveSheet
     wSheetStart.AutoFilterMode = False
     'Set a range variable to the correct item column
     Set rRange = Range("A1", Range("A65536").End(xlUp))
    
         'Delete any sheet called "UniqueList"
         'Turn off run time errors & delete alert
         On Error Resume Next
         Application.DisplayAlerts = False
         Worksheets("UniqueList").Delete
    
         'Add a sheet called "UniqueList"
         Worksheets.Add().Name = "UniqueList"
    
            'Filter the Set range so only a unique list is created
             With Worksheets("UniqueList")
                  rRange.AdvancedFilter xlFilterCopy, , _
                  Worksheets("UniqueList").Range("A1"), True
    
                  'Set a range variable to the unique list, less the heading.
                  Set rRange = .Range("A2", .Range("A65536").End(xlUp))
             End With
    
             On Error Resume Next
             With wSheetStart
                 For Each rCell In rRange
                   strText = rCell
                   .Range("A1").AutoFilter 1, strText
                     Worksheets(strText).Delete
                     'Add a sheet named as content of rCell
                     Worksheets.Add().Name = strText
                     'Copy the visible filtered range _
                     (default of Copy Method) and leave hidden rows
                     .UsedRange.Copy Destination:=ActiveSheet.Range("A1")
                     ActiveSheet.Cells.Columns.AutoFit
                 Next rCell
             End With
    
         With wSheetStart
             .AutoFilterMode = False
             .Activate
         End With
    
         On Error GoTo 0
         Application.DisplayAlerts = True
    End Sub
    Last edited by ashtanga; 08-12-2009 at 06:11 PM.

  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: Split worksheet into multiple worksheets

    Maybe like this:

        With wSheetStart
            For Each rCell In rRange
                strText = rCell
                .Range("A1").AutoFilter 1, strText
            
            'Clear sheet or create it if needed
                If Evaluate("ISREF('" & strText & "'!A1)") Then
                    Sheets(strText).Cells.ClearContents
                Else
                    Worksheets.Add().Name = strText
                End If
            
            'Copy the visible filtered range (default of Copy Method) and leave hidden rows
                .UsedRange.Copy Range("A1")
                Cells.Columns.AutoFit
             Next rCell
         End With
    _________________
    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
    Registered User
    Join Date
    08-12-2009
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Split worksheet into multiple worksheets

    Thanks, it nearly works. It puts my first sales person into the Unique worksheet and leaves that sales person's worksheet empty?

  4. #4
    Registered User
    Join Date
    08-12-2009
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Split worksheet into multiple worksheets

    And when I deleted a sales person worksheet, rerun the macro, the worksheet wasn't replaced.

  5. #5
    Registered User
    Join Date
    08-12-2009
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Split worksheet into multiple worksheets

    oops forget those comments I was running the macro from the worksheet

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

    Re: Split worksheet into multiple worksheets

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].


    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is 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