sorting to other worksheets

NatureGreen

New Member
Joined
Mar 12, 2019
Messages
7
Hi all

Every week i get a new report with over 600 employees.
Each of them are asigned to a coach. (Column I)

I have to select them and make a new worksheet per coach.
So that every coach has a clear list.

How can i automate this so that i can put the report on sheet 1 and that it automatically makes a new sheet for every coach?

Idk how i can add a better example but it's something like this but with more columns.

So it's based on the column Coach and the amount of employees can shift alot.

Employer
typecompanyGroupplaceid locationunitAdresCoach
namefirstnameWork ID
JOBA
fulltimePlace Adlfkjmsdfjdlfjiel108080Zone BStreetJohn
Person 1Firstname 11563050580
JOBB
Parttime
Place B
dmfkddlfkjmsdfj
108080Zone ALaneJohnPerson 2Firstname 21562885584
JOBB
fulltimePlace D
fmdjfoe
dmfkd
15568Zone DDriveJosyPerson 3Firstname 31562907624
JOBA
Parttime
Place Adfjldkjflfmdjfoe43984Zone CRoadJennyPerson 4Firstname 41562989780

<colgroup><col style="mso-width-source:userset;mso-width-alt:3072;width:66pt" width="88"> <col style="mso-width-source:userset;mso-width-alt:2141;width:46pt" width="61"> <col style="mso-width-source:userset;mso-width-alt:3025;width:65pt" width="87"> <col style="mso-width-source:userset;mso-width-alt:2466;width:53pt" width="71"> <col style="mso-width-source:userset;mso-width-alt:2327;width:50pt" width="67"> <col style="mso-width-source:userset;mso-width-alt:3211;width:69pt" width="92"> <col style="mso-width-source:userset;mso-width-alt:2048;width:44pt" width="59"> <col style="mso-width-source:userset;mso-width-alt:2420;width:52pt" width="69"> <col style="mso-width-source:userset;mso-width-alt:2536;width:55pt" width="73"> <col style="mso-width-source:userset;mso-width-alt:2350;width:51pt" width="67"> <col style="mso-width-source:userset;mso-width-alt:3025;width:65pt" width="87"> <col style="mso-width-source:userset;mso-width-alt:2769;width:60pt" width="79"> </colgroup><tbody>
</tbody>


Thanks in advance.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try


Code:
Sub movetosheet()
Dim lr As Long
Dim lrc As Long
Dim ws As Worksheet
lr = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For x = lr To 2 Step -1
    
    'test if sheet already exists
    Dim worksh As Integer
    Dim worksheetexists As Boolean
    worksh = Application.Sheets.Count
    worksheetexists = False
    For A = 1 To worksh
        If Worksheets(A).Name = Sheets("Sheet1").Cells(x, "I") Then
            worksheetexists = True
            Exit For
        End If
    Next A
    
    If worksheetexists = True Then
        newname = Sheets("Sheet1").Cells(x, "I")
        lrc = Sheets(newname).Cells(Rows.Count, 1).End(xlUp).Row + 1
        Sheets("Sheet1").Rows(x).Cut Sheets(newname).Cells(lrc, 1)
    Else
        newname = Sheets("Sheet1").Cells(x, "I")
        Sheets.Add(After:=Sheets(Sheets.Count)).Name = newname
        Sheets("Sheet1").Rows(1).Copy Sheets(newname).Range("A1")
        lrc = Sheets(newname).Cells(Rows.Count, 1).End(xlUp).Row + 1
        Sheets("Sheet1").Rows(x).Cut Sheets(newname).Cells(lrc, 1)
    
    End If
        
Next x
 
End Sub
 
Upvote 0
it works for 1 line. (the last one) But then it stops and gives an 1004 error.
So i got 1 extra sheet with the titles + 1 row with data.

Is it also possible to copy them to those sheets instead of pasting them to the new sheet? So i can keep the first sheets as a complete list.
 
Upvote 0
What line is highlighted when you debug?

You can change .cut to .copy to copy them to the new sheet, but if you run the code again it will copy the lines over again.
 
Upvote 0
The blue line is the one that gives an error. After adding 1 line to a new sheet.
Probably cause the sheet name doesn't have the name "Sheet1" anymore but the name of the Coach.


If worksheetexists = True Then
newname = Sheets("Sheet1").Cells(x, "J")
lrc = Sheets(newname).Cells(Rows.Count, 1).End(xlUp).Row + 1
Sheets("Sheet1").Rows(x).Cut Sheets(newname).Cells(lrc, 1)
Else
newname = Sheets("Overzicht_Alle").Cells(x, "J")
Sheets.Add(After:=Sheets(Sheets.Count)).Name = newname
Sheets("Overzicht_Alle").Rows(1).Copy Sheets(newname).Range("A1")
lrc = Sheets(newname).Cells(Rows.Count, 1).End(xlUp).Row + 1
Sheets("Overzicht_Alle").Rows(x).Cut Sheets(newname).Cells(lrc, 1)

End If
 
Upvote 0
The code does not rename any existing sheets only create new ones. What is in that cell? If the cell has a value that has characters that can not be used in a sheet name you will get that error.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,850
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top