Runtime error 9, but only with larger data set

bbott

Well-known Member
Joined
Feb 5, 2010
Messages
2,350
I have a macro that works when I have a data set that is about 1,500 rows or less. When I run the marcro on a data set with 2,000+ rows, I get the error 'Runtime error 9: Subscript out of range.' It errors on this line:

Code:
l = Sheets(CStr(y)).Cells(Rows.Count, "O").End(xlUp).Row + 1

Any ideas?

Code:
Sub Download()

    Dim x As Long
    Dim y As String
    Dim r As Range
    Dim l As Long

    ActiveSheet.Name = "Download"
    Range("E:E,G:G,H:H,K:K,M:M,O:O,P:P,U:U,Y:Y,Z:Z,AA:AC").Delete
    
    Worksheets.Add.Name = "Managers"
    Worksheets.Add.Name = "Assistant Managers 1"
    Worksheets.Add.Name = "Assistant Managers 2"
    Worksheets.Add.Name = "Team Leaders"
    Worksheets.Add.Name = "Team Members"
    
    Worksheets("Download").Select
    x = 2
    
    Do Until x > Sheets("Download").UsedRange.Rows.Count
        y = Sheets("Download").Cells(x, 15).Value
        Set r = Range("A" & x).EntireRow
        l = Sheets(CStr(y)).Cells(Rows.Count, "O").End(xlUp).Row + 1
        r.EntireRow.Copy Sheets(CStr(y)).Range("A" & l)
        x = x + 1
    Loop
           
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Subscript out of range indicates that it cannot find the object it has been told to use. In this case Sheets(CStr(y)).
Since y is dependent on Sheets("Download).Cells(x, 15).Value, if that value exceeds the limits of your data or is blank, then you will get the error message.
 
Upvote 0
Ah. I had one blank cell in the 2,500 rows of test data. Now it works.

Thanks for the assist!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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