Run-time error '9': Subscript out of range ERROR

programsam

Board Regular
Joined
Feb 10, 2016
Messages
123
Hey gang,

Getting a run-time error with this code. Attempting to create separate worksheets and copy rows based on data in column "B" but it's getting hung up on this line.

Code:
Set tws = Sheets(cel.Value)

When I look at values, all I get is tws = Nothing.

Here's the whole code.

Code:
Sub splitData()
     
     Dim sws As Worksheet
     Dim tws As Worksheet
     Dim cel As Range
     
     Set sws = Sheets("Summary for Export")
     
     For Each cel In sws.Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
         Set tws = Sheets(cel.Value)
         cel.EntireRow.Copy tws.Range("A" & Rows.Count).End(xlUp).Offset(1)
     Next cel
     
 End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi Mike, when I check the value... it shows as 1280 which is the value of the first row (B2) but it does not create a worksheet or add the row to the worksheet, nor does it move to the next row and add that value. Thoughts?

It looks like cel doesn't contain a sheet's name. Perhaps extra or missing spaces.
 
Upvote 0
Do you have a sheet named "1280"?
If not, you need to create code to create it. There is no sheet creation code in that routine.
This code adds an error handler to create a worksheet if one doesn't already exist.
(There is no handling for the situation where a cell contains an illegal sheet name.)

Code:
Sub splitData2()
    Dim sws As Worksheet
    Dim tws As Worksheet
    Dim cel As Range
    
    Set sws = Sheets("Summary for Export")
    
    For Each cel In sws.Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
        On Error GoTo MakeSheet
        Set tws = Sheets(cel.Value)
        On Error GoTo 0
        cel.EntireRow.Copy tws.Range("A" & Rows.Count).End(xlUp).Offset(1)
    Next cel
    
Exit Sub
MakeSheet:
    If Err = 9 Then
        Err.Clear
        With cel.Parent.Parent
            .Sheets.Add(after:=.Sheets(.Sheets.Count)).Name = CStr(cel.Value)
        End With
        Resume
    Else
        MsgBox "Error #" & Err & Error
        Err.Clear
    End If
End Sub
 
Upvote 0
Okay, now I've figured out why it didn't work.

It looks like the first three rows have data "1280" in column B so what's happening is the first row gets hit, the page is created and then VBA gives me an error after the second one because of the multiple occurrences of the same data.

How could this be written to only create a page off of the FIRST occurrence of "1280" and then move to the next row with different data, say "1290"?

Thanks Mike, I feel like I'm developing an understanding of what's going on but this one has got me stumped.
 
Upvote 0
Does this work?
Code:
Set tws = Sheets(CStr(cel.Value))
 
Upvote 0
Indeed it does Norie, thanks brother!

Last question, how would I carry over headers to row one on these pages from the page the data was copied from>
 
Upvote 0

Forum statistics

Threads
1,214,972
Messages
6,122,530
Members
449,088
Latest member
RandomExceller01

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