+ Reply to Thread
Results 1 to 4 of 4

Working with Workbook array

  1. #1
    Otto Moehrbach
    Guest

    Working with Workbook array

    Excel XP, WinXP
    I have a number of Excel files open, say One.xls, Two.xls, Three.xls. I
    want to do something with each file in turn in a For loop. The "Test1"
    macro has a "Type Mismatch" error in the "For" line. What is the correct
    way to write this "For" line? I finally went with the Test2 macro but it
    seems awkward. Thanks for your help. Otto
    Sub Test1()
    Dim wb As Workbook
    For Each wb In Workbooks(Array("One.xls", "Two.xls", "Three.xls"))
    MsgBox wb.Name
    Next wb
    End Sub

    Sub Test2()
    Dim wb As Workbook
    Dim wbName As Variant
    For Each wbName In Array("One.xls", "Two.xls", "Three.xls")
    MsgBox Workbooks(wbName).Name
    Next wbName
    End Sub



  2. #2
    Bob Phillips
    Guest

    Re: Working with Workbook array

    The array is an array of workbook names not workbooks, so it is bound to
    fail.

    Awkward maybe, but method 2 seems as good as it gets to me.

    --

    HTH

    Bob Phillips

    (replace xxxx in the email address with gmail if mailing direct)

    "Otto Moehrbach" <[email protected]> wrote in message
    news:#IydgT#[email protected]...
    > Excel XP, WinXP
    > I have a number of Excel files open, say One.xls, Two.xls, Three.xls. I
    > want to do something with each file in turn in a For loop. The "Test1"
    > macro has a "Type Mismatch" error in the "For" line. What is the correct
    > way to write this "For" line? I finally went with the Test2 macro but it
    > seems awkward. Thanks for your help. Otto
    > Sub Test1()
    > Dim wb As Workbook
    > For Each wb In Workbooks(Array("One.xls", "Two.xls", "Three.xls"))
    > MsgBox wb.Name
    > Next wb
    > End Sub
    >
    > Sub Test2()
    > Dim wb As Workbook
    > Dim wbName As Variant
    > For Each wbName In Array("One.xls", "Two.xls", "Three.xls")
    > MsgBox Workbooks(wbName).Name
    > Next wbName
    > End Sub
    >
    >




  3. #3
    AA2e72E
    Guest

    RE: Working with Workbook array

    Try:

    Sub Test1()
    Dim wb As Workbook
    For Each wb In Workbooks
    MsgBox wb.Name
    Next wb
    End Sub

    Workbooks is a collection of the open workbooks.


  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518
    try
    Please Login or Register  to view this content.

+ 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