Subscript out of range error on this code...

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
917
Office Version
  1. 365
Platform
  1. Windows
I keep getting "Subscript out of range" run time error code 9 on this code where indicated

Code:
Sub copyNonBlankData()
Dim erow As Long, lastrow As Long, i As Long
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
If Sheet1.Cells(i, 1) <> “” Then
Sheets(“Sheet1”).Range(Cells(i, 1), Cells(i, 2)).Copy  - Subscript out of range error at this line 
Sheets(“sheet2”).Activate
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Sheets(“sheet2”).Range(Cells(erow, 1), Cells(erow, 2))
Sheets(“sheet1”).Activate
End If
Next i
Application.CutCopyMode = False
End Sub


Can anyone see any reason why ?

Thanks for anyone's help

cr
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Your quote marks (") look strange and test, as copied from my browser, to be CHAR 147 not 34 which is the usual quote mark. To test replace this bit on the errant line:
Code:
Sheets(“Sheet1”).Range(....
with this
Code:
Sheet1.Range(....
Do you still get a code 9 error on that line?
 
Upvote 0
Your quote marks (") look strange and test, as copied from my browser, to be CHAR 147 not 34 which is the usual quote mark. To test replace this bit on the errant line:
Code:
Sheets(“Sheet1”).Range(....
with this
Code:
Sheet1.Range(....
Do you still get a code 9 error on that line?

….this is becoming very interesting. Did exactly what you said, but now a new error message appears that says Run time error 424 "object required" when I run the code with this line

Code:
Sheet1.Range(Cells(i, 1), Cells(i, 5)).Copy
What is Excel telling us it doesn't like ?
PS the only other thing I can tell you is that I am running this code block from a button on a userform. But would that make any difference?

cr


Thx for your help
cr
 
Last edited:
Upvote 0
I keep getting "Subscript out of range" run time error code 9 on this code where indicated

Rich (BB code):
Sub copyNonBlankData()
Dim erow As Long, lastrow As Long, i As Long
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
If Sheet1.Cells(i, 1) <> “” Then
Sheets(“Sheet1”).Range(Sheets(“Sheet1”).Cells(i, 1), Sheets(“Sheet1”).Cells(i, 2)).Copy  - Subscript out of range error at this line 
Sheets(“sheet2”).Activate
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Sheets(“sheet2”).Range(Cells(erow, 1), Cells(erow, 2))
Sheets(“sheet1”).Activate
End If
Next i
Application.CutCopyMode = False
End Sub


Can anyone see any reason why ?
Maybe you need to qualify the Cells call as shown in red above.
 
Upvote 0
Maybe you need to qualify the Cells call as shown in red above.
Hi Rick - sorry for getting back late - I had to test and retest this code to see if it would work. It doesn't. I changed my code
to reflect your changes in red. It got past that line fine. Please see explanation below. Sheet1 and Sheet2 were generic names. Below is the real
sheet names, and what I want to do along with the code that does not work:


REGISTER sheet has data entry values from A2:: K2 and data is currently on row 894 YTD. Column E or 5 has outstanding items not yet paid. They are NOT in continuous rows, so there are blank cells(rows) between items in this column.

I just want to copy ALL row values for which column E cell values are NOT BLANK from A to E of the REGISTER sheet to the REPORT sheet so I can generate an itemized report of all outstanding items and display the results in a userform (OUTITEMS) listbox. The REPORT sheet does not have to have a header row. The results can just be copied to cells A1:E1. Can this be done accurately using either a For Loop sequence or an Autofilter ?. The code below does not work correctly

Code:
Private Sub cmdCopyTest_Click()
Dim erow As Long, lastrow As Long, i As Long
lastrow = Sheets("REGISTER").Cells(Rows.Count, 5).End(xlUp).Row
For i = 2 To lastrow
If Sheets("REGISTER").Cells(i, 5) <> "" Then
Sheets("REGISTER").Range(Sheets("REGISTER").Cells(i, 1), Sheets("REGISTER").Cells(i, 5)).Copy 
Sheets("REPORT").Activate
erow = Sheets("REPORT").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Sheets("REPORT").Range(Cells(erow, 1), Cells(erow, 5)) <----bugs out at this line for some reason
Sheets("REGISTER").Activate
End If
Next i
End Sub

Run time error 1004 'Application defined or object defined error' is generated at this line:
ActiveSheet.Paste Destination:=Sheets("REPORT").Range(Cells(erow, 1), Cells(erow, 5))


I am sorry for this lengthy explanation.

cr
 
Upvote 0
Try this, but you might want to consider that when pasting you usually only need to specify the top left corner of the destination range, and you can specify the destination when using Copy.
Code:
With Sheets("REPORT")
    .Paste Destination:=.Range(.Cells(erow, 1), .Cells(erow, 5))
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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