Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
bw bw is offline
external usenet poster
 
Posts: 74
Default Can't select worksheet

The following code is called from Sheet2:

1. Worksheets("Sheet1").Unprotect
2. Worksheets("Sheet1").Activate
3. Cells(3, 1).Copy 'THIS CELL IS COPIED FROM SHEET2....and is WRONG
4. Worksheets("Sheet1").Cells(I, 1).Copy 'THIS CELL IS COPIED FROM
SHEET1....and is CORRECT

Why doesn't line 3 do the copy from Sheet1 as I had intended?

Thanks,
Bernie

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Can't select worksheet

When you do not explicitly specify sheets you leave things to chance and
prone to errors as code is modified... Try this

with Worksheets("Sheet1")
.Unprotect
.Activate
.Cells(3, 1).Copy
.Cells(I, 1).Copy
end with

Code in standard code modules refer to the active sheet unless otherwise
specified. Code in sheets refer to the sheet they are in unless otherwise
specified. That being said ALWAYS explictly reference the sheets you are
working with. It makes your code more readable and much less prone to run
time error.
--
HTH...

Jim Thomlinson


"bw" wrote:

The following code is called from Sheet2:

1. Worksheets("Sheet1").Unprotect
2. Worksheets("Sheet1").Activate
3. Cells(3, 1).Copy 'THIS CELL IS COPIED FROM SHEET2....and is WRONG
4. Worksheets("Sheet1").Cells(I, 1).Copy 'THIS CELL IS COPIED FROM
SHEET1....and is CORRECT

Why doesn't line 3 do the copy from Sheet1 as I had intended?

Thanks,
Bernie


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default Can't select worksheet

Because the code is in Sheet2, Cells(3,1) would refer to A3 in Sheet2, even
if Sheet1 was the activesheet. If it were in a code module, Cells(3,1) would
have referred to A3 in the activesheet. To be safe, always qualify your
ranges.

"bw" wrote:

The following code is called from Sheet2:

1. Worksheets("Sheet1").Unprotect
2. Worksheets("Sheet1").Activate
3. Cells(3, 1).Copy 'THIS CELL IS COPIED FROM SHEET2....and is WRONG
4. Worksheets("Sheet1").Cells(I, 1).Copy 'THIS CELL IS COPIED FROM
SHEET1....and is CORRECT

Why doesn't line 3 do the copy from Sheet1 as I had intended?

Thanks,
Bernie


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
Select corresponding row on another worksheet aehan Excel Programming 3 November 1st 06 02:31 PM
Can I create a worksheet menu to select each other worksheet pippagrace Excel Discussion (Misc queries) 4 June 23rd 06 01:28 PM
In Excel 2000, How do you select the whole of a worksheet (Select. Rascal Excel Discussion (Misc queries) 1 March 5th 05 12:03 AM
In Excel 2000, How do you select the whole of a worksheet (Select. Rascal Excel Discussion (Misc queries) 1 March 4th 05 11:59 PM
Select First Worksheet halem2[_16_] Excel Programming 4 September 15th 04 05:56 PM


All times are GMT +1. The time now is 08:56 PM.

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

About Us

"It's about Microsoft Excel"