+ Reply to Thread
Results 1 to 6 of 6

Why must I specify workbook here?

Hybrid View

  1. #1
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Why must I specify workbook here?

    I'm stepping through very basic VBA to re-learn some essentials.

    I have two workbooks open, each with a couple of worksheets. No duplicate names. The two open sheets are called Track Data and TEMP respectively.

    Why does this fail with subscript out of range please?

    Worksheets("Track Data").Activate
    It works if I specify the workbook.

    Workbooks("TEST track sheet copying.xlsm").Worksheets("Track Data").Activate
    That involves finding its name and carefully pasting it into my code, so I'd prefer not to do so. Why must I specify it in this situation please? In what circumstances can it be left out?

    Terry, East Grinstead, UK
    Last edited by zbor; 03-03-2017 at 06:01 AM.
    Terry, East Grinstead, UK
    Excel 365, Win 10 Pro, i7 PC, 4.0 GHz

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why must I specify workbook here?

    Oops, misread code.
    Last edited by Norie; 03-03-2017 at 06:21 AM.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Re: Why must I specify workbook here?

    Eh? Once again, I don't follow you! I asked why I have to specify the workbook.

    Obviously, as we established in another thread, if the workbook is included, as I did in the second case, then the extension has to be included! As I did.

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Why must I specify workbook here?

    If Track Data is not in the active workbook, you need to specify the workbook when you refer to it. Also, if the code is in the ThisWorkbook module of a different workbook, you need to specify the correct workbook- either by name or by ActiveWorkbook.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Re: Why must I specify workbook here?

    Quote Originally Posted by xlnitwit View Post
    If Track Data is not in the active workbook, you need to specify the workbook when you refer to it. Also, if the code is in the ThisWorkbook module of a different workbook, you need to specify the correct workbook- either by name or by ActiveWorkbook.
    Thanks, understood (I think!). So, would you recommend always specifying the correct workbook as a safe rule?

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Why must I specify workbook here?

    Yes. Even if you only specify ActiveWorkbook, it's better always to be specific.

+ 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. Assign name to Workbook, Copy range from one workbook to another workbook
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-18-2015, 03:40 AM
  2. [SOLVED] VBA to open new workbook and run macro from new workbook, which closes old workbook
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2014, 10:45 AM
  3. [SOLVED] Code to cut cell from one workbook, close same workbook, and paste in different workbook
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 03-13-2014, 04:01 PM
  4. Copy data one workbook to another workbook without opening workbook
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2014, 11:28 AM
  5. [SOLVED] Copy Values From Each Workbook in Folder to a Single Sheet in New Workbook +Workbook names
    By Arsham24 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-21-2012, 07:42 PM
  6. Save an open workbook, then open template workbook and close the saved workbook
    By ondvirg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-27-2009, 10:20 PM
  7. Replies: 1
    Last Post: 04-01-2006, 03:50 PM

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