Hello,
I'm new to VB, and cannot get my code to do what I need. I'm using Excel2013. My workbook has one sheet (Sheet1), with columns A - K, and 307 rows (one row per student). Column H is a teachers name. I need my code to create new sheets for each teacher (col H), and copy all student's data (col A:K) to the new sheet who have the same teacher. This would loop through all 307 rows of students. Essentially taking a large class list, and parsing it out by teacher.
I am getting a "subscript out of range" on this line: Sheets(strDestinationSheet).Visible = True
Here is my full code:
Thank you for your help.
I'm new to VB, and cannot get my code to do what I need. I'm using Excel2013. My workbook has one sheet (Sheet1), with columns A - K, and 307 rows (one row per student). Column H is a teachers name. I need my code to create new sheets for each teacher (col H), and copy all student's data (col A:K) to the new sheet who have the same teacher. This would loop through all 307 rows of students. Essentially taking a large class list, and parsing it out by teacher.
I am getting a "subscript out of range" on this line: Sheets(strDestinationSheet).Visible = True
Here is my full code:
Code:
Sub ImportToTeacherLists()
Dim strSourceSheet As String
Dim strDestinationSheet As String
Dim lastRow As Long
strSourceSheet = "Sheet1"
Sheets(strSourceSheet).Visible = True
Sheets(strSourceSheet).Select
Range("h2").Select
Do While ActiveCell.Value <> ""
strDestinationSheet = ActiveCell.Value
Selection.Copy
ActiveCell.Offset(0, 0).Resize(1, ActiveCell.CurrentRegion.Columns.Count).Select
Selection.Copy
Sheets(strDestinationSheet).Visible = True
Sheets(strDestinationSheet).Select
lastRow = Sheets(strDestinationSheet).Range("h2").End(xlDown).Row
Cells(lastRow + 1, 1).Select
Selection.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Sheets(strSourceSheet).Select
ActiveCell.Offset(0, 2).Select
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Thank you for your help.