+ Reply to Thread
Results 1 to 10 of 10

Macro - Split into Multiple Worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    02-26-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Question Macro - Split into Multiple Worksheets

    Hi,

    first post, hope I don't make too many gaffes

    I found this macro that does what I want but I need to change it slightly.

    It splits a big list onto different worksheets based on a category in column A.

    I've tried but I can't work out how to make it look in a different column. Here's the code I have now, I already improved it to work with long category names with spaces in them.

    Sub SplitIntoWorksheets() 
    Dim rRange As Range, rCell As Range 
    Dim wSheet As Worksheet 
    Dim wSheetStart As Worksheet 
    Dim strText As String 
    Dim strTitle 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 
    * * * * * * * * *  
    * * * * *  * * * * strTitle = Replace(strText, " ", "_")
    * * * * * * * * *  
    * * * * *  * * * * strTitle = Left(strTitle, 31)
    
    * * * * * *  * * *.Range("A1").AutoFilter 1, strText 
    
    * * * * * * * *  * * Worksheets(strTitle).Delete
    
    * * * * * *  * * * * 'Add a sheet named as content of rCell  
    
    * * * * * *  * * * * Worksheets.Add().Name = strTitle
    
    * * * *  * * * * * * '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

  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: Macro - Split into Multiple Worksheets

    This now restuctured so you only have to set the one variable "fCol" (for column) at the top of the macro. If you want column A, set fCol = 1, for column D, set fCol = 4.
    Option Explicit
    
    Sub SplitIntoWorksheets()
    'Declare variables
    Dim rRange As Range, rCell As Range
    Dim wSheet As Worksheet, wSheetStart As Worksheet
    Dim strTitle As String, fCol As Long
    
    'Speed up execution
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
    
    'Set activesheet to a variable name so we can refer to it easily
        Set wSheetStart = ActiveSheet
    
    'Turn off the Autofilter in case it got left on accidentally
        wSheetStart.AutoFilterMode = False
    
    'Enter the column # here to evaluate, column A = 1
        fCol = 3       
      
    'Set a range out the values in the chosen column
        Set rRange = Range(Cells(1, fCol), Cells(Rows.Count, fCol).End(xlUp))
     
    'Check if "UniqueList" sheet exists
        If Not Evaluate("ISREF(UniqueList!A1)") Then
            Worksheets.Add().Name = "UniqueList"        'add it if needed
        Else
            Worksheets("UniqueList").Cells.Clear        'clear it if it exists already
        End If
        
    'Filter the Set rRange so unique item list is created
        With Worksheets("UniqueList")
            rRange.AdvancedFilter xlFilterCopy, , Worksheets("UniqueList").Range("A1"), True
    
    'Set the rRange variable to the unique list of values, without the heading
            Set rRange = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
        End With
    
    'Starting with the original data sheet again...
        With wSheetStart
        '...Evaluate the rRange items against the data sheet one unique value at a time
            For Each rCell In rRange
            'create a version of the value with no spaces in it so it can be used as sheetname
                strTitle = Left(Replace(rCell, " ", "_"), 31)
            'Filter the original data by the field:=fCol and the criteria1:=rcell
                .Range("A1").AutoFilter fCol, rCell
            
            'Test to see if a sheet already exists for this value
                If Not Evaluate("ISREF('" & strTitle & "'!A1)") Then
                    Worksheets.Add().Name = strTitle     'add it if needed
                Else
                    Worksheets(strTitle).Cells.Clear     'clear it if it exists
                End If
        
            'Copy filtered data (visible data only) to the new/cleared sheet    
                .UsedRange.Copy Destination:=Worksheets(strTitle).Range("A1")
    
            'Clean up the new sheet's appearance
                Worksheets(strTitle).Cells.Columns.AutoFit
    
            'Loop around to the next unique value
            Next rCell
    
        'When all values are processed, turn off the Autofilter in the data
            .AutoFilterMode = False
    
        'Return to the data sheet
            .Activate
        End With
    
    'reactivate application settings turned off earlier for speed
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    End Sub
    Last edited by JBeaucaire; 02-27-2010 at 12:03 PM.
    _________________
    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
    02-26-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Macro - Split into Multiple Worksheets

    Thanks Jerry, I'll be trying that on Monday when i get back to the office.

    Any chance of commenting your code so I can understand what it's doing?

    Thanks again.
    John.

  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: Macro - Split into Multiple Worksheets

    Code above edited to add more comments.

  5. #5
    Registered User
    Join Date
    02-26-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Macro - Split into Multiple Worksheets

    Just tested that at work and it's spot on...

    thanks again Jerry, that's gonna save me hours!

  6. #6
    Registered User
    Join Date
    05-18-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Macro - Split into Multiple Worksheets

    I have just tried it out and it is going to make my life a lot easier Jerry... Thanks a lot

  7. #7
    Registered User
    Join Date
    09-16-2010
    Location
    Glasgow
    MS-Off Ver
    2010
    Posts
    68

    Re: Macro - Split into Multiple Worksheets

    Brilliant, it just works perfect for me too. Thanks Jerry.

  8. #8
    Registered User
    Join Date
    11-13-2012
    Location
    UAE
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Macro - Split into Multiple Worksheets

    'Samall addition to select the column to split , no need of hard coding column number.
    enter column # through input box.
    'Enter the column # here to evaluate, column A = 1
    fCol = InputBox("Split column number")

  9. #9
    Registered User
    Join Date
    03-04-2013
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Macro - Split into Multiple Worksheets

    I was looking for something like this and found many macros, but this one works the best. Thanks a lot!

  10. #10
    Registered User
    Join Date
    07-19-2013
    Location
    Washington, DC
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Macro - Split into Multiple Worksheets

    This was VERY HELPFUL, THANK YOU!!!!

+ 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