Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 79
Default error - select method failed - (columns.select)

Getting a 1004 error - select method of range class failed - with the
following code:

Sub unhide()

Dim sh As Object


For Each sh In ActiveWorkbook.Worksheets

sh.Visible = True

sh.Columns.Select

Selection.EntireColumn.Hidden = False

Next sh

End Sub

the code is in a module. The macro works for the first sheet of the
three-sheet workbook then bombs on the second sheet

Thanks in advance

PB
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default error - select method failed - (columns.select)

When you use a For Each loop with the Worksheets collection, the individual
sheets are *not* automatically made active as the loop spins. Also, you
cannot Select a range that is not on the active sheet. Together, these facts
cause the line

sh.Columns.Select

to fail. The sh variable refers to a worksheet that is not active and then
Select attempts to select a range on the sh worksheet. Use

sh.Select
sh.Columns.Select

Of course, it isn't necessary to Select anything, so you can use just

sh.Columns.EntireColumn.Hidden = False

within the loop.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




"PBcorn" wrote in message
...
Getting a 1004 error - select method of range class failed - with the
following code:

Sub unhide()

Dim sh As Object


For Each sh In ActiveWorkbook.Worksheets

sh.Visible = True

sh.Columns.Select

Selection.EntireColumn.Hidden = False

Next sh

End Sub

the code is in a module. The macro works for the first sheet of the
three-sheet workbook then bombs on the second sheet

Thanks in advance

PB


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default error - select method failed - (columns.select)

Hi P,

Your error is encountered because you
cannot make selections on an inactive
sheet.

Try the following version:

'===========
Sub unhide()
Dim sh As Worksheet

For Each sh In ActiveWorkbook.Worksheets
sh.Columns.Hidden = False
Next sh
End Sub
'<<===========


---
Regards.
Norman
"PBcorn" wrote in message
...
Getting a 1004 error - select method of range class failed - with the
following code:

Sub unhide()

Dim sh As Object


For Each sh In ActiveWorkbook.Worksheets

sh.Visible = True

sh.Columns.Select

Selection.EntireColumn.Hidden = False

Next sh

End Sub

the code is in a module. The macro works for the first sheet of the
three-sheet workbook then bombs on the second sheet

Thanks in advance

PB


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 79
Default error - select method failed - (columns.select)

thanks. as you can tell I am in the early stages of learning VB.

"Chip Pearson" wrote:

When you use a For Each loop with the Worksheets collection, the individual
sheets are *not* automatically made active as the loop spins. Also, you
cannot Select a range that is not on the active sheet. Together, these facts
cause the line

sh.Columns.Select

to fail. The sh variable refers to a worksheet that is not active and then
Select attempts to select a range on the sh worksheet. Use

sh.Select
sh.Columns.Select

Of course, it isn't necessary to Select anything, so you can use just

sh.Columns.EntireColumn.Hidden = False

within the loop.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




"PBcorn" wrote in message
...
Getting a 1004 error - select method of range class failed - with the
following code:

Sub unhide()

Dim sh As Object


For Each sh In ActiveWorkbook.Worksheets

sh.Visible = True

sh.Columns.Select

Selection.EntireColumn.Hidden = False

Next sh

End Sub

the code is in a module. The macro works for the first sheet of the
three-sheet workbook then bombs on the second sheet

Thanks in advance

PB


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
Excel VBA error - Select method of Range class failed R Ormerod Excel Programming 0 July 12th 07 09:02 PM
Run-Time error '1004' : Select method of Range class failed [email protected] Excel Discussion (Misc queries) 3 March 9th 07 01:36 PM
Error: method 'select' of object_worksheet' failed Carl Excel Discussion (Misc queries) 4 September 9th 06 08:52 PM
RT Error 1004, Select method of worksheet class failed Dana Sherbondy Excel Programming 2 October 1st 04 03:55 PM
error 1004 Select method of Range class failed J.E. McGimpsey Excel Programming 1 September 12th 03 07:42 PM


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