I am trying to copy the 2nd row from 99 sheets and add them one after the other in the 100th sheet. The vba code is below, however I keep getting the following error:
Run-time error'9': Subscript out of range
Sub copyrow()
Dim Nrow As Long, Nsheet As Long
Dim i As Long
Nrow = 2
Nsheet = 100
For i = 1 To Nsheet - 1
Sheets(i).Cells(Nrow, 1).EntireRow.Copy Sheets(Nsheet).Cells(i, 1)
Next i
End Sub
The sheet names are Sheet1 (2),... Sheet1 (24).. etc until Sheet1 (99)
I am new to vba and don't know how to fix this error, I suspect it might be in the naming convention but I am not sure.
Nsheet
put there"Sheet1 (" & Nsheet & ")"
- TeamothySheet1 (2)
toSheet1 (99)
is98
sheets. Is the tab name of the last sheetSheet1(100)
(maybe you ought to start withFor i = 2 To Nsheet - 1
after adopting Teamothy's suggestion? Please do clarify. - VBasic2008MsgBox ThisWorkbook.Sheets.Count
? - Siddharth RoutNrow
is not incrementing? If it is not supposed to increment then get rid of that unnecessary variable and changeSheets(i).Cells(Nrow, 1).EntireRow.Copy
toSheets(i).Rows(2).Copy
BTW I am not sure what are you trying to copy and what are you trying to achieve? - Siddharth Rout