Subscript Out Of Range

AngelK

New Member
Joined
Aug 4, 2016
Messages
34
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:
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.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Welcome to the board.

That means there is no sheet in the active workbook whose name is the value of strDestinationSheet. It cannot mean anything else.
 
Upvote 0
Also, make sure the active cell

Code:
strDestinationSheet = ActiveCell.Value

does not contain any blanks, or non allowable characters for sheet names !!
 
Upvote 0
Commented code below. Hope I got it right.

Code:
Public Sub ImportToTeacherLists()

Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastRows As New Collection
Dim teacherName As String
Dim lastRow As Long
Dim thisRow As Long
Dim targetRow As Long

On Error Resume Next

' Assume we're starting with the teacher sheet
Set sourceSheet = ActiveSheet

' Find the last row in the source sheet
With sourceSheet
    lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
End With

' Loop through the rows
For thisRow = 2 To lastRow
    ' Get the teacher name
    teacherName = sourceSheet.Cells(thisRow, 8).Value
    
    ' Try and get the sheet for this teacher
    Err.Clear
    Set targetSheet = Worksheets(teacherName)
    
    ' Check error return
    If Err.Number <> 0 Then
        ' Sheet not found - we've not created it yet so do it now and set the name
        Set targetSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
        targetSheet.Name = teacherName
        
        ' Copy the headers from the source sheet
        sourceSheet.Range("A1:K1").Copy Destination:=targetSheet.Range("A1")
        
        ' Add to the collection
        lastRows.Add 2, teacherName
        
        ' Set the target row
        targetRow = 2
    Else
        ' Get the target row
        targetRow = lastRows.Item(teacherName)
    End If
    
    ' Copy this line across
    sourceSheet.Cells(thisRow, 1).Resize(, 11).Copy Destination:=targetSheet.Cells(targetRow, 1)
    
    ' Increment the target row and update the collection
    targetRow = targetRow + 1
    lastRows.Remove teacherName
    lastRows.Add targetRow, teacherName
Next thisRow

End Sub

WBD
 
Upvote 0
Thank you to everyone for the quick response. This code worked perfectly. Saved me another day of frustration! I appreciate all of your help. Have a great weekend!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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