ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Selecting a range in a different worksheet (https://www.excelbanter.com/excel-programming/292162-selecting-range-different-worksheet.html)

Bob Chisholm

Selecting a range in a different worksheet
 
The code for a command button on SheetA says:

Worksheets("SheetB").Activate
Range("B2").Select

The "TakeFocusOnClick" property of the command button is set to false.

When the button on SheetA is clicked, the active worksheet switches OK
to
SheetB, but the Range("B2").Select statement generates an error '1004':
"Select method of Range class failed"

Does the code for selecting on SheetB need to be in a separate location
than
the code for SheetA's command button, such as in a Worksheet.Activate
event?

Or what else have I overlooked, left out, or done wrong?

Thanks for any help.


pikus

Selecting a range in a different worksheet
 
Try this instead:

Application.GoTo Worksheets("SheetB").Cells(2, 2)

- Pikus


---
Message posted from http://www.ExcelForum.com/


Tom Ogilvy

Selecting a range in a different worksheet
 
Worksheets("SheetB").Activate
Worksheets("SheetB").Range("B2").Select

the unqualified Range("B2").Select when used in a sheet module, infers that
Range("B2") is referencing B2 on the sheet containing the code. Since that
is not your intent and the sheet containing the code is not the activesheet,
you need to qualify where B2 is located.

--
Regards,
Tom Ogilvy


"Bob Chisholm" wrote in message
...
The code for a command button on SheetA says:

Worksheets("SheetB").Activate
Range("B2").Select

The "TakeFocusOnClick" property of the command button is set to false.

When the button on SheetA is clicked, the active worksheet switches OK
to
SheetB, but the Range("B2").Select statement generates an error '1004':
"Select method of Range class failed"

Does the code for selecting on SheetB need to be in a separate location
than
the code for SheetA's command button, such as in a Worksheet.Activate
event?

Or what else have I overlooked, left out, or done wrong?

Thanks for any help.





All times are GMT +1. The time now is 11:25 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com