+ Reply to Thread
Results 1 to 7 of 7

How to avoid using ThisWorkbook?

  1. #1
    Registered User
    Join Date
    01-29-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    32

    How to avoid using ThisWorkbook?

    Hello,

    I'm using a routine that opens .xls files copies them and closes them back up. The problem is that I'm referring to the workbook I ran the procedure from as "ThisWorkbook" and if I have other Excel files open the routine fails. Is there a way to avoid using ThisWorkbook?

    Thanks for your suggestion in advance
    Last edited by LenaK; 02-01-2015 at 09:36 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,908

    Re: How to avoid using ThisWorkbook?

    Not sure I understand your problem. ThisWorkbook always refers to the workbook that contains the code that is currently running. As opposed to the ActiveWorkbook, which is the currently active/selected workbook.

    It is good practice to define and set variables to refer to the source and target workbooks when you want to copy between them. That ensures you don't experience the confusion that it seems you are doing. You also need to fully qualify any references to cells in the source and target workbooks.

    Difficult to be more specific without seeing the code ... ideally with sample workbooks.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    01-29-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    32

    Red face Re: How to avoid using ThisWorkbook?

    Sorry I am new here.

    I would like avoid hard-coding the workbook name, so if someone accidentally names the workbook differently the code will not work. The workbook is attached.

    Thank you so much for your advice.
    Attached Files Attached Files

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,908

    Re: How to avoid using ThisWorkbook?

    I don't think that it is "ThisWorkbook" that is giving you the problem. It's more likely:

    Please Login or Register  to view this content.
    Using a workbook's index is quite dangerous as opening and closing workbooks mean it can relate to a workbook other than the workbook you think it does.

    You should use something like this to refer to ThisWorkbook:

    Please Login or Register  to view this content.
    and:

    Please Login or Register  to view this content.

    In that way, you can explicitly refer to, or Select (if you must, although you rarely need to), the source and target workbooks.



    Regards, TMS

  5. #5
    Registered User
    Join Date
    01-29-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    32

    Re: How to avoid using ThisWorkbook?

    So when I refer to the Workbook being copied from I should say:

    Please Login or Register  to view this content.
    Correct?

  6. #6
    Registered User
    Join Date
    01-29-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    32

    Re: How to avoid using ThisWorkbook?

    Brilliant! it worked!!!

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,908

    Re: How to avoid using ThisWorkbook?

    You're welcome. Thanks for the rep.

+ 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. ThisWorkbook Help
    By OcMac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-19-2012, 05:36 PM
  2. [SOLVED] Set Wb = ThisWorkbook
    By ATLGator in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-01-2012, 02:10 PM
  3. ThisWorkbook
    By DejaVu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-22-2006, 08:55 PM
  4. Help with ThisWorkbook
    By David in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-27-2005, 07:05 PM
  5. [SOLVED] RE: Help with ThisWorkbook
    By K Dales in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-27-2005, 05:05 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