PC Review


Reply
Thread Tools Rate Thread

autofit in multiple workbooks

 
 
=?Utf-8?B?RWxhaW5lIEou?=
Guest
Posts: n/a
 
      19th Aug 2007
I have 16 workbooks open. I want to go through each one and autofit the
columns. Specifically I want to autofit the columns, then save and close
each workbook. The workbooks are already named and saved once, but I need to
resave the changes.

This is the code that I am using:

Dim wsheet As Range
Dim wb As Workbook
Dim wWorkbooks As Collection

For Each wb In Workbooks

Set wsheet = ActiveWorkbook.Worksheets("Sheet1").Range("A1:AG25")

With wsheet
.Select
.Columns.AutoFit
.save
.close
End With

next wb


It will autofit the columns on about three of the files and save (but not
close). Then it just stops.

Any help would be appreciated.

Elaine
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      19th Aug 2007
The for statement doesn't activate the workbook, just give a list of workbooks
You need the following change (add wb.name)

For Each wb In Workbooks

Set wsheet = workbooks(wb.name).Worksheets("Sheet1").Range("A1:AG25")


"Elaine J." wrote:

> I have 16 workbooks open. I want to go through each one and autofit the
> columns. Specifically I want to autofit the columns, then save and close
> each workbook. The workbooks are already named and saved once, but I need to
> resave the changes.
>
> This is the code that I am using:
>
> Dim wsheet As Range
> Dim wb As Workbook
> Dim wWorkbooks As Collection
>
> For Each wb In Workbooks
>
> Set wsheet = ActiveWorkbook.Worksheets("Sheet1").Range("A1:AG25")
>
> With wsheet
> .Select
> .Columns.AutoFit
> .save
> .close
> End With
>
> next wb
>
>
> It will autofit the columns on about three of the files and save (but not
> close). Then it just stops.
>
> Any help would be appreciated.
>
> Elaine

 
Reply With Quote
 
=?Utf-8?B?RWxhaW5lIEou?=
Guest
Posts: n/a
 
      19th Aug 2007
Joel, thanks for your reply, but I am afraid I am confused. For instance
say I have three workbooks (I actually will have 17) open (ie. Arts.xls,
Crafts.xls and Sewing.xls). Are you saying that I have to name each one of
the workbooks that are opened? I guess I was under the impression that
ActiveWorkbook took the place of naming the actual workbook. I thought I
could work with the workbook that was active, save it and the next workbook
would be active. Obviously I am missing something here.

Can you give me a specific example using the workbooks named above?

Thanks so much.

"Joel" wrote:

> The for statement doesn't activate the workbook, just give a list of workbooks
> You need the following change (add wb.name)
>
> For Each wb In Workbooks
>
> Set wsheet = workbooks(wb.name).Worksheets("Sheet1").Range("A1:AG25")
>
>
> "Elaine J." wrote:
>
> > I have 16 workbooks open. I want to go through each one and autofit the
> > columns. Specifically I want to autofit the columns, then save and close
> > each workbook. The workbooks are already named and saved once, but I need to
> > resave the changes.
> >
> > This is the code that I am using:
> >
> > Dim wsheet As Range
> > Dim wb As Workbook
> > Dim wWorkbooks As Collection
> >
> > For Each wb In Workbooks
> >
> > Set wsheet = ActiveWorkbook.Worksheets("Sheet1").Range("A1:AG25")
> >
> > With wsheet
> > .Select
> > .Columns.AutoFit
> > .save
> > .close
> > End With
> >
> > next wb
> >
> >
> > It will autofit the columns on about three of the files and save (but not
> > close). Then it just stops.
> >
> > Any help would be appreciated.
> >
> > Elaine

 
Reply With Quote
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      19th Aug 2007
You just had to change the one line that I had shown below. Doing the FOR wb
just references each workbook, it doesn't make the workbook the active
workbook. You just need to reference the new wb. Look at the changes below,
you'll understand.

Dim wsheet As Range
Dim wb As Workbook
Dim wWorkbooks As Collection

For Each wb In Workbooks

'old code
'Set wsheet = ActiveWorkbook.Worksheets("Sheet1").Range("A1:AG25")
'new code
Set wsheet = workbooks(wb.name).Worksheets("Sheet1").Range("A1:AG25")

With wsheet
.Select
.Columns.AutoFit
.save
.close
End With

next wb



"Elaine J." wrote:

> Joel, thanks for your reply, but I am afraid I am confused. For instance
> say I have three workbooks (I actually will have 17) open (ie. Arts.xls,
> Crafts.xls and Sewing.xls). Are you saying that I have to name each one of
> the workbooks that are opened? I guess I was under the impression that
> ActiveWorkbook took the place of naming the actual workbook. I thought I
> could work with the workbook that was active, save it and the next workbook
> would be active. Obviously I am missing something here.
>
> Can you give me a specific example using the workbooks named above?
>
> Thanks so much.
>
> "Joel" wrote:
>
> > The for statement doesn't activate the workbook, just give a list of workbooks
> > You need the following change (add wb.name)
> >
> > For Each wb In Workbooks
> >
> > Set wsheet = workbooks(wb.name).Worksheets("Sheet1").Range("A1:AG25")
> >
> >
> > "Elaine J." wrote:
> >
> > > I have 16 workbooks open. I want to go through each one and autofit the
> > > columns. Specifically I want to autofit the columns, then save and close
> > > each workbook. The workbooks are already named and saved once, but I need to
> > > resave the changes.
> > >
> > > This is the code that I am using:
> > >
> > > Dim wsheet As Range
> > > Dim wb As Workbook
> > > Dim wWorkbooks As Collection
> > >
> > > For Each wb In Workbooks
> > >
> > > Set wsheet = ActiveWorkbook.Worksheets("Sheet1").Range("A1:AG25")
> > >
> > > With wsheet
> > > .Select
> > > .Columns.AutoFit
> > > .save
> > > .close
> > > End With
> > >
> > > next wb
> > >
> > >
> > > It will autofit the columns on about three of the files and save (but not
> > > close). Then it just stops.
> > >
> > > Any help would be appreciated.
> > >
> > > Elaine

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Summarizing multiple workbooks to other multiple workbooks BK Microsoft Excel Programming 0 17th May 2010 08:43 PM
Rows().AutoFit starts a calculation, but Columns().AutoFit doesn't Stefano Microsoft Excel Programming 1 30th Nov 2009 05:01 PM
RE: Rows().AutoFit starts a calculation, but Columns().AutoFit doesn't Mike H Microsoft Excel Programming 0 25th Nov 2009 04:31 PM
Updating Workbooks from multiple links Workbooks TimJames Microsoft Excel Worksheet Functions 1 15th Dec 2007 03:34 PM
Multiple workbooks open -- closing workbooks =?Utf-8?B?SmFuZXQgUGFuaWdoZXR0aQ==?= Microsoft Excel Discussion 3 13th Nov 2007 05:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:48 AM.