LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #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


 
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 11:26 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"