I have done a small project, which consists of 5 excel sheet in, code is working fine and I am getting exact result also, but if I rename sheets from sheet1 to some other name I am getting Subscript out of range Error.
What is the reason for this and what needs to be done to overcome this. Please help.
Below is the code
Public Sub amount_final()
Dim Row1Crnt As Long
Dim Row2Crnt As Long
With Sheets("sheet4")
Row1Last = .Cells(Rows.Count, "B").End(xlUp).Row
End With
Row1Crnt = 2
With Sheets("sheet3")
Row2Last = .Cells(Rows.Count, "B").End(xlUp).Row
End With
There is nothing wrong with the code per se. You will get
Subscript out of rangeerror if Excel is not able to find a particular sheet which is quite obvious since you renamed it. For example, if you rename your sheet “Sheet3” to “SheetXYZ” then Excel will not be able to find it.The only way to avoid these kind of errors is to use CODENAME of the sheets. See Snapshot
Here we have a sheet which has a name “Sample Name before Renaming”
So consider this code
The same code can be written as
Now no matter how many times you rename the sheet, the above code will always work 🙂
HTH
Sid