+ Reply to Thread
Results 1 to 22 of 22

Splitting a worksheet into separate worksheets based on a keyword in a cloumn

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    I need to split a worksheet based on a keyword in a column. There are 9000 rows and it takes around 3 hours to do this manually. I have code that splits the worksheets but I will have to do this 18 times and it will take too long. Here is my code for splitting worksheet. I need to implement this into a loop to loop 18 times for 18 different keywords but don't know how. Very new to VB and macros

    Formula: copy to clipboard
    Sub SearchForString()

    Dim LSearchRow As Integer
    Dim LCopyToRow As Integer

    On Error GoTo Err_Execute

    'Start search in row 7
    LSearchRow = 7

    'Start copying data to row 7 in Sheet2 (row counter variable)
    LCopyToRow = 7

    While Len(Range("A" & CStr(LSearchRow)).Value)

    'If value in column E = "KEYWORD", copy entire row to Sheet2
    If Range("A" & CStr(LSearchRow)).Value = "KEYWORD" Then

    'Select row in Sheet1 to copy
    Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
    Selection.Copy

    'Paste row into Sheet2 in next row
    Sheets("Sheet2").Select
    Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
    ActiveSheet.Paste

    'Move counter to next row
    LCopyToRow = LCopyToRow + 1

    'Go back to Sheet1 to continue searching
    Sheets("Sheet1").Select

    '______________

    'Position on cell A3
    Application.CutCopyMode = False
    Range("A3").Select

    MsgBox "All matching data has been copied."

    Exit Sub

    Err_Execute:
    MsgBox "An error occurred."

    End Sub
    Last edited by boomboomblock; 06-26-2013 at 11:22 AM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    Use auto-filter, or better attach your sample. To attach a sample, go to advance then attachment.

  3. #3
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    I need to use a macro so that it is full automated. I have provided some dummy data in the attachment, my spreadsheet is over 9000 rows long and there are 18 sections I must extract. Imagine this data on a much larger scale and they all need to be split into indivudual sheets for analysis purposes. Unfortunately I don't think it is as easy as simply putting an auto-filter in
    Attached Files Attached Files

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    Put a header in A1, eg AREA and try this code.
    Option Explicit
    
    Sub DistributeRows()
    Dim wsAll As Worksheet
    Dim wsCrit As Worksheet
    Dim wsNew As Worksheet
    Dim LastRow As Long
    Dim LastRowCrit As Long
    Dim I As Long
        
        Set wsAll = Worksheets("Sheet1") ' change All to the name of the worksheet the existing data is on
        
        LastRow = wsAll.Range("A" & Rows.Count).End(xlUp).Row
        
        Set wsCrit = Worksheets.Add
        
        ' column A has the criteria eg project ref
        wsAll.Range("A1:A" & LastRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsCrit.Range("A1"), Unique:=True
        
        LastRowCrit = wsCrit.Range("A" & Rows.Count).End(xlUp).Row
        For I = 2 To LastRowCrit
        
            Set wsNew = Worksheets.Add
            wsNew.Name = wsCrit.Range("A2")
            wsAll.Rows("1:" & LastRow).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=wsCrit.Range("A1:A2"), _
             CopyToRange:=wsNew.Range("A1"), Unique:=False
            wsCrit.Rows(2).Delete
            
        Next I
        
        Application.DisplayAlerts = False
        wsCrit.Delete
        Application.DisplayAlerts = True
        
    End Sub
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    That seems to be working for that problem hopefully I can use it on my other table! Thanks a lot Norie and AB33

  6. #6
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    The code is making new sheets with the names I need on them but doesn't seem to be pulling all the information out of the original sheet onto my new ones

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    It seemed to work fine with the sample workbook.

    A new worksheet was created for each Area and the data for each Area was copied to the appropriate sheet.

    What's different about the sheet you are trying it on?

  8. #8
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    alternative suggestion?
    Sub miscreant() 
    
    Const cl& = 1
    Dim a As Variant, q As Variant
    Dim rws&, cls&, p&, i&, b As Boolean
    Application.ScreenUpdating = False
    With Sheets.Add(after:=Sheets("sheet1"))
        Sheets("sheet1").Cells(1).CurrentRegion.Copy .Cells(1)
        Set a = .Cells(1).CurrentRegion
        rws = a.Rows.Count
        cls = a.Columns.Count
        a.Sort a(1, cl), Header:=xlYes
        .Name = a(2, cl)
        a = a.Resize(rws + 1)
        p = 2
        For i = p To rws + 1
            If a(i, cl) <> a(p, cl) Then
                If b Then
                    Sheets.Add.Name = a(p, cl)
                    .Cells(p, 1).Resize(i - p, cls).Cut Cells(2, 1)
                    Sheets("sheet1").Cells(1).Resize(, cls).Copy Cells(1)
                End If
                b = True
                p = i
            End If
        Next i
    End With
    Application.ScreenUpdating = True
    
    End Sub

  9. #9
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    Yeah it worked fine for the sample sheet. These are some of the headings I will be using in my actual sheet. There are also around 9000 rows for the document I want split. I unfortunately cannot send the full document as it has names of customers etc on it
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    Try the attached.
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    I think works with your sample, but need to adjust the headings. I have the heading in row and goes 4 columns across. On the word attached, I think your heading is in row 5, or 6 and stretches up to column M. Please confirm if I am right and will adjust my code.

  12. #12
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    I need to sort it by column A. My headings go to column R but I was unable to show that as it was too big for the word document as it was too big

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    Where is the heading? Is it in row 5, or 6?

  14. #14
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    The heading is in row 6

  15. #15
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    I am not sure what do you mean by you want to sort by column A. I thought we are creating tabs based on column A. If this is not the case, it is a wrong code.
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    Yes, sorry that's what I meant. It is doing the job I want it to do in my exampleBook but it is not working for my other document. The other document is larger and has more columns than my exampleBook

  17. #17
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    I have added another code which could accommodate any columns and rows size and is much faster.
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    Thanks, which macro is it, alice or createnames

  19. #19
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    alice and name of the module is latestmod.

  20. #20
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    Thanks AB33, it works a treat. Cheers everyone for all their help, very much appreciated!

  21. #21
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    You are welcome!

    Could you please now close (Mark) this thread as solved? Go to the top right-hand side of this page, choose "Thread Tools" from the menu, then select "solved" from the drop down menu.

  22. #22
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Splitting a worksheet into separate worksheets based on a keyword in a cloumn

    The code I posted should work for any no of fields/columns as long as you put a heading in A1 which can easily be done with code.

    If the headings aren't in row 1 then there would need some further, small, adjustments.

    Here's the updated code.
    Option Explicit
    
    Sub DistributeRows()
    Dim wsAll As Worksheet
    Dim wsCrit As Worksheet
    Dim wsNew As Worksheet
    Dim LastRow As Long
    Dim LastRowCrit As Long
    Dim I As Long
    Dim HeaderRow As Long
    
        Set wsAll = Worksheets("Sheet1") ' change All to the name of the worksheet the existing data is on
        
        ' change to whatever row the headers are in
        HeaderRow = 6
        
        wsAll.Range("A1").Value = "Field1"
        
        LastRow = wsAll.Range("A" & Rows.Count).End(xlUp).Row
        
        Set wsCrit = Worksheets.Add
        
        ' column A has the criteria eg project ref
        wsAll.Range("A" & HeaderRow & ":A" & LastRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsCrit.Range("A1"), Unique:=True
        
        LastRowCrit = wsCrit.Range("A" & Rows.Count).End(xlUp).Row
        For I = 2 To LastRowCrit
        
            Set wsNew = Worksheets.Add
            wsNew.Name = wsCrit.Range("A2")
            wsAll.Rows(HeaderRow & ":" & LastRow).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=wsCrit.Range("A1:A2"), _
             CopyToRange:=wsNew.Range("A1"), Unique:=False
             wsNew.Range("A1").Value = ""
            wsCrit.Rows(2).Delete
            
        Next I
        
        wsAll.Range("A1").Value = ""
        
        Application.DisplayAlerts = False
        wsCrit.Delete
        Application.DisplayAlerts = True
        
    End Sub

+ 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