+ Reply to Thread
Results 1 to 2 of 2

Referencing Workbooks within VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    04-07-2014
    Location
    Mahwah, New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    25

    Referencing Workbooks within VBA

    In a script, I am working with two workbooks. The problem occurs with using the Activate function. When the script was first set up, the developer simply wrote Workbooks(1).Activate. For a given session, this may have worked well but depending on what workbooks you have open at the time, the index reference for the desired workbook may not be "1". Hence, I replaced the reference with the workbook name as in: Workbook("Hold_Request")Activate.

    This worked out fine for awhile but then started giving errors. Why? Even though "Hold_Request" was the proper name of the workbook, within VBA it was called "Hold_Request (15)". I never had more than one workbook open at a time by that name. So why do the numbers get appended to the project name?

    The only solution I see at the moment is to do a while for all open projects to see which one has a name with it's first twelve characters equal to "Hold_Request". I could then either use the expanded name or the index for use with my Activate function. Any ideas on this matter? Thanks in advance.

    Steve

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: Referencing Workbooks within VBA

    Hi, Steve,

    shouldn´t that be
    Workbooks("Hold_Request.xls")Activate
    indicating that the extension should be part of the name as well (I assumed a 2003 version spreadsheet here)?

    Or set objects to the workbook like
    Dim wbHold As Workbook
    
    Set wbHold = Workbooks("Hold_Request.xls")
    and refer to wbHold or ThisWorkbook which is the workbook that holds the code. You may also check if the object holds a reference for a workbook prior to working on in order to avoid run-time errors.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Referencing other workbooks through VBA
    By davidman1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2013, 09:31 AM
  2. referencing between two workbooks
    By iainiow in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-28-2010, 12:28 PM
  3. Referencing 2 workbooks
    By jcherian in forum Excel General
    Replies: 1
    Last Post: 07-01-2009, 01:45 PM
  4. Referencing workbooks
    By pmguerra in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2008, 12:19 PM
  5. [SOLVED] Referencing Other Workbooks
    By Rick B in forum Excel General
    Replies: 3
    Last Post: 05-15-2006, 11:20 AM

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