Consulting

Results 1 to 16 of 16

Thread: Solved: Workbook open problem

  1. #1

    Solved: Workbook open problem

    I found some code on a web site. When I tried it, I got a problem. I searched to find the resolution and found another web site with the identical statement:
    [vba] Dim wbMine as Workbook
    ...
    Set wbMine = Workbooks("C:\data\Test4.xls")
    ...
    [/vba]
    The error I'm getting is that "Workbooks" has a bad index. There is a workbook with that name on the indicated drive and directory. What gives?

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Workbooks("workbookName") expects that a workbook named "workbookName" has been opened by the same instance of Excel that is running the code.
    (I think that "There is a file with that name on the indicated drive and directory." is the prefered MicroSoft terminology.)

    Each workbook refered to by WorkBook("name") needs to be open.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Dim wbMine As Workbook
    On Error Resum Next
    Set wbMine = Workbooks("Test4.xls")
    On Error Goto 0
    If wbMine Is Nothing Then
    Set wbMine = Workbooks.Open("C:\data\Test4.xls")
    End If
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4

    Thanks to all responders

    Thanks for the information. It does make sense, but it seems strange to me that it works this way. I have no idea why you would want VBA to access an already-open workbook. Wouldn't you already have access to the data?

    The supplied code is interesting... a way around VBA's behavior, or on a more positive note, a solution within the given framework.

    Thanks again.

  5. #5
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    The main purpose of VBA is to automate tasks. Obviously it will be extremely useful for VBA to be able to access and manipulate data in open workbooks for this! Otherwise, what would be the need for VBA?
    Regards,
    Rory

    Microsoft MVP - Excel

  6. #6

    Poorly worded complaint

    Thanks for the insight, Rory.

    I think I did a poor job of stating my complaint. It just seemed odd that an already accessed spreadsheet would need to be accessed again from VBA code in a different workbook. I think maybe I didn't consider things like "add-ins" where the code you need is in the add-in and not in the workbook which the add-in processes. Otherwise, you could only process data in the same workbook as the code. Also, since you can have multiple workbooks open, and you might want to alter the data in a particular workbook from this add-in, this is the way you tell VBA which of the open workbooks to choose. Am I close? I guess my complaint is not very well founded!

  7. #7
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Correct. As a general rule, if you don't specify which workbook/worksheet to act on, code will run against the active sheet in the active workbook.
    Regards,
    Rory

    Microsoft MVP - Excel

  8. #8
    Thanks again for the help.

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by rory
    As a general rule...
    General? How about all the darn time.. LOL!

  10. #10
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Not necessarily in a worksheet module!
    Regards,
    Rory

    Microsoft MVP - Excel

  11. #11
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    ROFL! Well if that's not the most literal assumption....

  12. #12
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    As is often the case:

    "I hate that my code does what I TELL IT TO DO instead of WHAT I WANT IT TO DO"

    how is excel supposed to know what you "mean" if you don't tell it..
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why not? MS seems to insist that thye know what I want to do and how I want to do it, so why shouldn't Excel know what I want to do?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Well while they're at it they can make me dinner too.

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    When you can stop looking in the mirror!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    At what, that devilishly handsome fellow? I see no reason...

Posting Permissions

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