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: 35,218
Default For each Workbook limit?

I had an error in the code.
The workbook has to be activated before the worksheet can be selected:

Dim wb As Workbook
Dim wsheet As Worksheet 'not just an object

For Each wb In Application.Workbooks
wb.activate 'added
Set wsheet = wb.Worksheets("sheet1")
With wsheet
.select 'if you want to select a cell, you have to select the sheet
.Columns("A:AG").Columns.AutoFit
.range("a2").select
End With
wb.save
wb.close savechanges:=false
Next wb

Elaine J. wrote:

Thanks for your response. I tried your code and I got a "runtime error €˜1004
method €˜select of object_worksheet failed. Was I supposed to add
something after .select??

Thanks so much for your help.

Elaine

"Dave Peterson" wrote:

I don't think your code does what you want.

I'd be more careful with what belongs to what--what worksheet belongs to what
workbook and what range belongs to what worksheet.

Dim wb As Workbook
Dim wsheet As Worksheet 'not just an object

For Each wb In Application.Workbooks
Set wsheet = wb.Worksheets("sheet1")
With wsheet
.select 'if you want to select a cell, you have to select the sheet
.Columns("A:AG").Columns.AutoFit
.range("a2").select
End With
wb.save
wb.close savechanges:=false
Next wb

Elaine J. wrote:

I recently posted a question regarding setting up a For each workbook
statement. It it basically working, but I have a follow up question.

1. I have 17 workbooks open at one time. I am attempting to go through
each workbook and format the columns to autofit. The code works for 9 of the
documents and then stops. This is the code that I am using.

Dim wb As Workbook
Dim wsheet As Object

For Each wb In Application.Workbooks
Set wsheet = Worksheets("sheet1")

With wsheet

Columns("A:AG").Select
Selection.Columns.AutoFit
ActiveCell.Offset(1, 0).Select

End With

ActiveWorkbook.Save
ActiveWorkbook.Close

Next wb

2. I have noticed that the nine workbooks that work are the 9 that show up
in the drop down list under the Windows menu. (I'm not sure that has
anything to do with it, but that is the way it appears to me.)

So my question is: Is there a limit on how many workbooks you can have open
and use the For Each method? And if so, how do I get around it? Or is there
something wrong with my code?

Thanks,

Elaine


--

Dave Peterson


--

Dave Peterson


 
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
Worksheet Limit in Workbook Ron Excel Worksheet Functions 2 June 30th 09 07:41 PM
Limit similtaneous users of workbook mekkor Excel Discussion (Misc queries) 2 January 4th 07 10:07 PM
Limit on # of worksheets in a workbook HTC Excel Discussion (Misc queries) 4 August 26th 05 05:29 PM
Limit use Of Workbook GWB Direct Excel Discussion (Misc queries) 6 May 3rd 05 11:02 PM
Limit on number of Sheets in a Workbook? Mr B[_3_] Excel Programming 1 April 8th 04 08:03 PM


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