+ Reply to Thread
Results 1 to 5 of 5

Split worksheet into multiples and add subtotals to each WS

Hybrid View

  1. #1
    Registered User
    Join Date
    07-13-2012
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2010
    Posts
    8

    Split worksheet into multiples and add subtotals to each WS

    I have this macro that splits one worksheet into multiple worksheets based upon content from Column A.
    Does anyone know how to add subtotals to each worksheet either using this formula or by some other method that will work (other than actually going into each worksheet to do so).

    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
    Thank you
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    03-15-2007
    Location
    AL, USA
    MS-Off Ver
    2007
    Posts
    174

    Re: Split worksheet into multiples and add subtotals to each WS

    look right after you autofit the data, and the code is there

    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
                     Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4), _
                     Replace:=True, PageBreaks:=False, SummaryBelowData:=True
                 Next rCell
             End With
    
         With wSheetStart
             .AutoFilterMode = False
             .Activate
         End With
    
         On Error GoTo 0
         Application.DisplayAlerts = True
    End Sub

  3. #3
    Registered User
    Join Date
    07-13-2012
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Split worksheet into multiples and add subtotals to each WS

    That completely worked, and blew my mind! That was awesome to see it work!

    Thank you!

  4. #4
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Split worksheet into multiples and add subtotals to each WS

    If you want to change the sheets to follow from A - Z you can use.

    Sub SortSheets()
       Application.ScreenUpdating = False
       Dim I As Integer, J As Integer
    
       For I = 1 To Sheets.Count - 1  'count sheets / take sheet 1 
        For J = I + 1 To Sheets.Count ' Pak 1 tabblad verder
             If UCase(Sheets(I).Name) > UCase(Sheets(J).Name) Then 'If sheet 1 greater in than sheet 2
                Sheets(J).Move before:=Sheets(I) 'change them
             End If
         Next J
      Next I
      Sheets("Is Clinic purchasing").Move before:=Sheets(1)
      
    
      Application.DisplayAlerts = True
      
    End Sub
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  5. #5
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Split worksheet into multiples and add subtotals to each WS

    And if you use the change below, you don't see the macro work.

    Dim strText As String
    
    Application.ScreenUpdating = False
    
     Set wSheetStart = ActiveSheet

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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