View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
NickHK NickHK is offline
external usenet poster
 
Posts: 4,391
Default Getting Run-time error although code looks correct

Forgot to add:
So yes, with your posted code you will get internittent error depending on
where this code resides.

Sheets("Payment Sales Master").Range(Rows(2),
Rows(2).End(xlDown)).ClearContents

- If in the "Payment Sales Master" WS, OK, as Rows refers to the same sheet
as Range.
- If in a module AND "Payment Sales Master" is the ActiveSheet, OK, as Rows
refers to the same sheet as Range.
- If in a module AND "Payment Sales Master" is NOT the ActiveSheet, ERROR,
as Rows refers to the ActiveSheet, whilst as .Range refers to
Sheets("Payment Sales Master").

NickHK

"robs3131" wrote in message
...
Hi,

I have been running into an issue for years since I first started coding

in
VBA within Excel -- it has driven me crazy and significantly effects my
ability to write code without having to record a macro...the issue appears

to
be interment and just seems to not make logical sense.

The issue is that for some reason, when I write, for example, the code

below
sometimes it does not work and I get the error listed below. When,

however,
I copy the same line of code from another sub, where that same code works,
THEN the error stops coming up. It is literally that the code is

identical -
it is just that when I copy it from another sub where the code has worked
INSTEAD of typing the code out, then the code works. Any insight would be
GREATLY appreciated as this is killing me -- it is like water torture --

it
has gotten to the point where I can't take it any more :) I want to
understand what the issue is and how I can avoid it.

Code:
Sheets("Payment Sales Master").Range(Rows(2),
Rows(2).End(xlDown)).ClearContents

Error:
Run-time error '1004':

Application-defined or object-defined error

--
Robert