Hello, folks. I'm getting an error : "Run-time error '1004':
Hi Ian KR,
I'm guessing that N L is getting the error because the code is being
run from some other worksheet, or maybe some other workbook, not from
a worksheet named "Actuals-Sheet".
If you are getting the code to work you must be running it from a
worksheet named "Actuals-Sheet". Cells on its own assumes Cells in the
active worksheet, so when run from the sheet named "Actuals-Sheet"
there is no problem. When run from some other worksheet silly Excel
grinds to a halt because Cells hasn't been told that the cells are in
another worksheet. To stop this happening Cells needs to be prefaced
with the worksheet containing the cells...
Worksheets("Actuals-Sheet').Cells
This has to be done for both Cells, making the code line much longer,
so it is usually done using a With/End With.
You're absolutely right. I'm running it from a normal code module, but it
only works if "Actuals-Sheet" is the active sheet, otherwise it throws up
that error. If I precede it with:
ActiveWorkbook.Worksheets("Actuals-Sheet").Activate
it works fine, whichever is the active sheet to start with. So, the sheet in
question has to be the active one. (Of course, because I'm running XL2003, I
have to change the second Cells(...) value to put the range inside 256
columns.)
So I guess that N L has to first activate the relevant workbook (if not
already) and then the activate the "Actuals-Sheet" worksheet before (s)he
can set the range reference. The annoying/irritating thing is that, at first
sight, N L's code does put the Range unambiguously in the right sheet, since
Range is qualified with the relevant sheet, but (as you say in your other
post) you ALSO have to qualify Cells(...) with the relevant sheet as well!
Doesn't Excel VBA work in mysterious ways!