Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 188
Default Cells.Select - Why not in a module?


Hi All,

Can anyone explain why the following code does not work in a module:


Sub SelAll()

For Each Sht In ActiveWorkbook.Worksheets

Sht.Cells.Select

Next

End Sub



Whereas, the following code works fine in a worksheet code:

Sub SelSht()

Cells.Select

End Sub


I am guessing it is related to scope, but I cannot see why from my
reading of the excel help files.

Thanks in advance,

Alan.



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 218
Default Cells.Select - Why not in a module?

You cannot select cells in other than the active sheet.
You must first select the sheet before selecting the
cells. For example, this works:

Sub SelAll()
Dim Sht As Worksheet
For Each Sht In ActiveWorkbook.Worksheets
Sht.Select
Sht.Cells.Select
Next
End Sub

Your second code example works because it references the
active sheet. The cells method, when not qualified,
refers to the active sheet; i.e. the
statement "Cells.Select" by default refers to the cells of
the active sheet.

Regards,
Greg


-----Original Message-----

Hi All,

Can anyone explain why the following code does not work

in a module:


Sub SelAll()

For Each Sht In ActiveWorkbook.Worksheets

Sht.Cells.Select

Next

End Sub



Whereas, the following code works fine in a worksheet

code:

Sub SelSht()

Cells.Select

End Sub


I am guessing it is related to scope, but I cannot see

why from my
reading of the excel help files.

Thanks in advance,

Alan.



.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 188
Default Cells.Select - Why not in a module?

"Greg Wilson" wrote in
message ...

You cannot select cells in other than the active sheet.
You must first select the sheet before selecting the
cells. For example, this works:

Sub SelAll()
Dim Sht As Worksheet
For Each Sht In ActiveWorkbook.Worksheets
Sht.Select
Sht.Cells.Select
Next
End Sub

Your second code example works because it references the
active sheet. The cells method, when not qualified,
refers to the active sheet; i.e. the
statement "Cells.Select" by default refers to the cells of
the active sheet.


Thanks Greg,

Much appreciated.

Regards,

Alan.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Cells.Select - Why not in a module?

Alan

As far as I know you can not select on a sheet that is not active


try

Sub SelAll()
Dim sHt As Worksheet
For Each sHt In ActiveWorkbook.Worksheets
sHt.Activate
sHt.Cells.Select
Next
End Su

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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 188
Default Cells.Select - Why not in a module?

"mudraker " wrote in
message ...

Alan

As far as I know you can not select on a sheet that is not active


try

Sub SelAll()
Dim sHt As Worksheet
For Each sHt In ActiveWorkbook.Worksheets
sHt.Activate
sHt.Cells.Select
Next
End Sub


Hi Mudraker,

Thanks - much appreciated.

Regards,

Alan.





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
Up down arrow keys do not select cells if select locked cells unch roandrob Excel Discussion (Misc queries) 3 May 18th 09 12:48 AM
Macro to select cells without a certain value and select a menu it Guy[_2_] Excel Worksheet Functions 9 January 2nd 09 05:21 PM
Using formulas to select cells (Ex: Select every nth cell in a col Lakeview Photographic Services Excel Discussion (Misc queries) 2 March 15th 07 02:17 PM
code in module A to not execute a Worksheet_SelectionChange sub of another module Jack Sons Excel Discussion (Misc queries) 4 December 11th 05 11:52 PM
how do you "select locked cells" w/o "select unlocked cells"? princejohnpaulfin Excel Discussion (Misc queries) 3 July 16th 05 03:53 AM


All times are GMT +1. The time now is 06:52 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"