+ Reply to Thread
Results 1 to 5 of 5

using variable for open workbooks

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    678

    using variable for open workbooks

    I am conversant with excel vba

    my code statements like this

    dim imp1  as workbook, imp2 as workbook,j as integer
    set imp1=workbopks("mybook1.xls")
    set imp2=workbooks("mybook2.xls")
    for j =1 to 2
    
    with workbooks ("imp" &  j)
    the last line gives error "subscription out of range"


    how to solve the problem(personal.xls file is also open and the above code is parked in that file)
    Last edited by venkat1926; 12-18-2010 at 12:52 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: using variable for open workbooks

    "imp" & j is not the name of a workbook.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    678

    Re: using variable for open workbooks

    moderator

    thank you for that clarification. I want to loop through the two open workbooks(actually there are 6 workbooks opened).. how to write the code.?

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: using variable for open workbooks

    Hi venkat1926
    this will loop through the open workbook collection
    Dim wb As Workbook
    For Each wb In Workbooks
     Workbooks(wb.Name).Activate
    Next
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: using variable for open workbooks

    If you want to iterate through selected worbooks by name,

        Dim v           As Variant
    
        For Each v In Array("bob.xls", "joe.xls")
            Workbooks(v).Activate
        Next v

+ 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