+ Reply to Thread
Results 1 to 5 of 5

Looping thru Workbooks problem

  1. #1
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    315

    Looping thru Workbooks problem

    Can someone explain why this code generates "Object doesn't support this property or method" error?

    Sub LoopThruWkBooksWkSheets()
    Dim wb as WorkBook
    Dim ws as WorkSheet

    For Each wb In Workbooks
    For Each ws In Worksheets
    wb.ws.Cells.ColumnWidth = 3 ----> error line
    Next
    Next
    End Sub

    The idea is to loop through all open workbooks and all worksheets therein to reset the columnwidths to 3. The above code can be tweaked to work by activating each Workbook as in:

    Sub LoopThruWkBooksWkSheets()
    Dim wb as WorkBook
    Dim ws as WorkSheet

    For Each wb In Workbooks
    wb.activate
    For Each ws In Worksheets
    ws.Cells.ColumnWidth = 3
    Next
    Next
    End Sub

    ...but this is arguably not elegant even by turning off screenupdating.


    TIA

    David
    Last edited by davidm; 04-13-2006 at 01:17 AM.

  2. #2
    Gary Keramidas
    Guest

    Re: Looping thru Workbooks problem

    use something like this

    Option Explicit
    Sub LoopThruWkBooksWkSheets()
    Dim wb As Workbook
    Dim ws As Worksheet

    For Each wb In Workbooks
    For Each ws In Worksheets
    With ws
    ..Cells.ColumnWidth = 3 ' ----> error line
    End With
    Next
    Next
    End Sub

    --


    Gary


    "davidm" <[email protected]> wrote in message
    news:[email protected]...
    >
    > Can someone explain why this code generates "Object doesn't support this
    > property or method" error?
    >
    > Sub LoopThruWkBooksWkSheets()
    > Dim wb as WorkBook
    > Dim ws as WorkSheet
    >
    > For Each wb In Workbooks
    > For Each ws In Worksheets
    > wb.ws.Cells.ColumnWidth = 3 ----> error line
    > Next
    > Next
    > End Sub
    >
    > The idea is to loop through all open workbooks and all worksheets
    > therein to reset the columnwidths to 3. The above code can be tweaked
    > to work by activating each Workbook as in:
    >
    > Sub LoopThruWkBooksWkSheets()
    > Dim wb as WorkBook
    > Dim ws as WorkSheet
    >
    > For Each wb In Workbooks
    > wb.activate
    > For Each ws In Worksheets
    > ws.Cells.ColumnWidth = 3 ----> error line
    > Next
    > Next
    > End Sub
    >
    > ..but this is arguably not elegant even by turning off
    > screenupdating.
    >
    >
    > TIA
    >
    > David
    >
    >
    > --
    > davidm
    > ------------------------------------------------------------------------
    > davidm's Profile:
    > http://www.excelforum.com/member.php...o&userid=20645
    > View this thread: http://www.excelforum.com/showthread...hreadid=532532
    >




  3. #3
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    315
    Gary,

    Your code works on only the ActiveWorkBook. Not surprising, considering the fact that the other workbooks are not looped.

    david

  4. #4
    Tim Williams
    Guest

    Re: Looping thru Workbooks problem

    You should qualify the Worksheets reference.

    '########################
    For Each wb In Workbooks
    For Each ws In wb.Worksheets
    ws.Cells.ColumnWidth = 3
    Next ws
    Next wb
    '########################


    Tim


    "davidm" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Can someone explain why this code generates "Object doesn't support this
    > property or method" error?
    >
    > Sub LoopThruWkBooksWkSheets()
    > Dim wb as WorkBook
    > Dim ws as WorkSheet
    >
    > For Each wb In Workbooks
    > For Each ws In Worksheets
    > wb.ws.Cells.ColumnWidth = 3 ----> error line
    > Next
    > Next
    > End Sub
    >
    > The idea is to loop through all open workbooks and all worksheets
    > therein to reset the columnwidths to 3. The above code can be tweaked
    > to work by activating each Workbook as in:
    >
    > Sub LoopThruWkBooksWkSheets()
    > Dim wb as WorkBook
    > Dim ws as WorkSheet
    >
    > For Each wb In Workbooks
    > wb.activate
    > For Each ws In Worksheets
    > ws.Cells.ColumnWidth = 3 ----> error line
    > Next
    > Next
    > End Sub
    >
    > ..but this is arguably not elegant even by turning off
    > screenupdating.
    >
    >
    > TIA
    >
    > David
    >
    >
    > --
    > davidm
    > ------------------------------------------------------------------------
    > davidm's Profile:
    > http://www.excelforum.com/member.php...o&userid=20645
    > View this thread: http://www.excelforum.com/showthread...hreadid=532532
    >




  5. #5
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    315
    Thanks Tim. As simple as that!

    David

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1