Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 144
Default Getting Run-time error although code looks correct

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Getting Run-time error although code looks correct

works here, sure the sheet is not protected?

With Sheets("Payment Sales Master")
.Unprotect
.Range(Rows(2), Rows(2).End(xlDown)).ClearContents
.Protect
End With
End Sub


--


Gary


"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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Getting Run-time error although code looks correct

or maybe this if it's not the active sheet

Sub test()
Dim ws As Worksheet
Set ws = Worksheets("Payment Sales Master")
With ws
.Unprotect
.Range(ws.Rows(2), ws.Rows(2).End(xlDown)).ClearContents
.Protect
End With
End Sub

--


Gary


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
works here, sure the sheet is not protected?

With Sheets("Payment Sales Master")
.Unprotect
.Range(Rows(2), Rows(2).End(xlDown)).ClearContents
.Protect
End With
End Sub


--


Gary


"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





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

To help solve these kind of problems of referencing cells on the correct WS,
you need to understand how Excel determines the Range you mean when
unqualifies ranges are used.
Maybe the code the code below will help:

'<WS1 Code
Private Sub CommandButton1_Click()

Worksheets(1).Activate
Call SheetTest_Module

Worksheets(2).Activate
Call SheetTest_Module

Call SheetTest_WS

Call Sheet2.SheetTest_WS

End Sub

Public Sub SheetTest_WS()
Debug.Print Range("A1").Parent.Name
'Name that contains this code: = Me.Name
End Sub
'<WS1 Code

'<WS2 Code
Public Sub SheetTest_WS()
Debug.Print Range("A1").Parent.Name
'Name that contains this code: = Me.Name
End Sub
'<WS2 Code

'<Module Code
Public Sub SheetTest_Module()
Debug.Print Range("A1").Parent.Name
'Name of ActiceSheet
End Sub
'</Module Code

The most robust way to avoid these error is to fully qualify the
ranges/cells that you are referring to.
Notice the addition of the ".", so the Rows are no longer unqualified:

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

Of course, if you really need the code to refer to whatever is the
ActiveSheet or the sheet that the code resides on, then use the unqualified
method in the appropriate location.

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



  #5   Report Post  
Posted to microsoft.public.excel.programming
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





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 144
Default Getting Run-time error although code looks correct

Thanks Nick. I think you are right in that I was not understanding how Excel
determines the Range. Based on your explanation below as well as your
subsequent post on this issue, it sounds like the Range I was setting was
unqualified, which I assume means that it is not tied to one sheet - it is
tied to whichever sheet is active - is that right? Again, from your
explanation, it sounds like the way I can aovid this is by using the With
loop with "." which I assume then ties the Range to the sheet bein referenced
in the With statement. Is this correct?

Thanks so much for your input.

--
Robert


"NickHK" wrote:

To help solve these kind of problems of referencing cells on the correct WS,
you need to understand how Excel determines the Range you mean when
unqualifies ranges are used.
Maybe the code the code below will help:

'<WS1 Code
Private Sub CommandButton1_Click()

Worksheets(1).Activate
Call SheetTest_Module

Worksheets(2).Activate
Call SheetTest_Module

Call SheetTest_WS

Call Sheet2.SheetTest_WS

End Sub

Public Sub SheetTest_WS()
Debug.Print Range("A1").Parent.Name
'Name that contains this code: = Me.Name
End Sub
'<WS1 Code

'<WS2 Code
Public Sub SheetTest_WS()
Debug.Print Range("A1").Parent.Name
'Name that contains this code: = Me.Name
End Sub
'<WS2 Code

'<Module Code
Public Sub SheetTest_Module()
Debug.Print Range("A1").Parent.Name
'Name of ActiceSheet
End Sub
'</Module Code

The most robust way to avoid these error is to fully qualify the
ranges/cells that you are referring to.
Notice the addition of the ".", so the Rows are no longer unqualified:

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

Of course, if you really need the code to refer to whatever is the
ActiveSheet or the sheet that the code resides on, then use the unqualified
method in the appropriate location.

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




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 144
Default Getting Run-time error although code looks correct

Thanks Gary. Based on your post and Nick's post, I'm going to use the With
statement as I now understand that this ties the range being defined to the
sheet in the With statement.

Thanks!

--
Robert


"Gary Keramidas" wrote:

or maybe this if it's not the active sheet

Sub test()
Dim ws As Worksheet
Set ws = Worksheets("Payment Sales Master")
With ws
.Unprotect
.Range(ws.Rows(2), ws.Rows(2).End(xlDown)).ClearContents
.Protect
End With
End Sub

--


Gary


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
works here, sure the sheet is not protected?

With Sheets("Payment Sales Master")
.Unprotect
.Range(Rows(2), Rows(2).End(xlDown)).ClearContents
.Protect
End With
End Sub


--


Gary


"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






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
#Value! error on code that should be correct? RHein Excel Discussion (Misc queries) 2 January 3rd 08 03:19 AM
Run-time error '9' ---- Code to fix included. [email protected] Excel Programming 3 September 2nd 05 02:48 PM
Run-time error from my code Matt Excel Programming 5 June 24th 04 03:32 AM
Code Error - Run Time Error 5 (Disable Cut, Copy & Paste) Tim[_36_] Excel Programming 4 April 23rd 04 02:53 AM
Code Run-time error '1004' pjhageman[_5_] Excel Programming 3 January 11th 04 12:24 AM


All times are GMT +1. The time now is 01:54 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"