Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Range refernce question...

If I am referring to a range not on the active sheet, why does this work
triggered from a Macro button on Sheet1...

Sheets("Sheet2").Range("A2:G57").ClearContents

But this will not?

Sheets("Sheet2").Range(Cells(2, 1), Cells(endrow, 7)).ClearContents

If I am on Sheet2, the above code works.

Why?




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 644
Default Range refernce question...

Jay, Cells also has a parent object, which is a sheet. You are in
effect actually saying this:
Sheets("Sheet2").Range(ActiveSheet.Cells(2, 1),
ActiveSheet.Cells(endrow, 7)).ClearContents 'which makes no sense to
excel
This will work:
With Sheets("Sheet2")
.Range(.Cells(2,1),.Cells(endrow,7)).ClearContents
End With

Charles Chickering
jayklmno wrote:
If I am referring to a range not on the active sheet, why does this work
triggered from a Macro button on Sheet1...

Sheets("Sheet2").Range("A2:G57").ClearContents

But this will not?

Sheets("Sheet2").Range(Cells(2, 1), Cells(endrow, 7)).ClearContents

If I am on Sheet2, the above code works.

Why?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Range refernce question...

Thanks!

"Die_Another_Day" wrote:

Jay, Cells also has a parent object, which is a sheet. You are in
effect actually saying this:
Sheets("Sheet2").Range(ActiveSheet.Cells(2, 1),
ActiveSheet.Cells(endrow, 7)).ClearContents 'which makes no sense to
excel
This will work:
With Sheets("Sheet2")
.Range(.Cells(2,1),.Cells(endrow,7)).ClearContents
End With

Charles Chickering
jayklmno wrote:
If I am referring to a range not on the active sheet, why does this work
triggered from a Macro button on Sheet1...

Sheets("Sheet2").Range("A2:G57").ClearContents

But this will not?

Sheets("Sheet2").Range(Cells(2, 1), Cells(endrow, 7)).ClearContents

If I am on Sheet2, the above code works.

Why?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Range refernce question...

If you are referencing another sheet, you have to qualify all objects with
the sheet identifier, otherwise part of the statement refers to the other
sheet, part refers to the active sheet.

Use

Sheets("Sheet2").Range(Sheets("Sheet2")..Cells(2, 1),
Sheets("Sheet2")..Cells(endrow, 7)).ClearContents

or better

With Sheets("Sheet2")
.Range(.Cells(2, 1), .Cells(endrow, 7)).ClearContents
End With

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"jayklmno" wrote in message
...
If I am referring to a range not on the active sheet, why does this work
triggered from a Macro button on Sheet1...

Sheets("Sheet2").Range("A2:G57").ClearContents

But this will not?

Sheets("Sheet2").Range(Cells(2, 1), Cells(endrow, 7)).ClearContents

If I am on Sheet2, the above code works.

Why?






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 200
Default Range refernce question...

jayklmno wrote:
If I am referring to a range not on the active sheet, why does this work
triggered from a Macro button on Sheet1...

Sheets("Sheet2").Range("A2:G57").ClearContents

But this will not?

Sheets("Sheet2").Range(Cells(2, 1), Cells(endrow, 7)).ClearContents

If I am on Sheet2, the above code works.

Why?




Assuming endrow=57, because Cells(2,1) and Cells(endrow,7) are, by
default, referring to cells on the active sheet.

There are several ways to avoid the problem. Perhaps the clearest that
indicates the difficulty is

Sheets("Sheet2").Range(Sheets("Sheet2").Cells(2, 1), _
Sheets("Sheet2").Cells(endrow, 7)).ClearContents

A common fix is

With Sheets("Sheet2")
..Range(.Cells(2, 1), .Cells(endrow, 7)).ClearContents
End With

I prefer

Set rng = Sheets("Sheet2").Range("A1")
Range(rng(2, 1), rng(endrow, 7)).ClearContents

Alan Beban


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
How do I enable drill down to refernce cells? MFINE Excel Discussion (Misc queries) 5 July 20th 07 12:32 AM
Circular Refernce Kamal Excel Discussion (Misc queries) 1 February 27th 07 04:07 PM
Circular Refernce error Robthemanbob Excel Discussion (Misc queries) 3 August 28th 06 06:14 PM
Named SUM Formula with relative refernce(s) Werner Rohrmoser Excel Worksheet Functions 2 April 20th 05 04:56 PM
How do I refernce a column as I would a field nam in access Rob H[_4_] Excel Programming 1 May 12th 04 10:07 PM


All times are GMT +1. The time now is 07:09 AM.

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"