+ Reply to Thread
Results 1 to 8 of 8

Two workbooks open, code changing wrong Wb, help!

  1. #1
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Two workbooks open, code changing wrong Wb, help!

    Hi, I have the following code and at line 200 the wrong workbook is having cells unprotected. How do I make the "final" workbook change and not the "Backup"? Having a brain fart today Thanks for the help!

    Please Login or Register  to view this content.
    Last edited by thecdnmole; 12-19-2020 at 03:19 PM.

  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: Two workbooks open, code changing wrong Wb, help!

    You need to add a workbook reference to the code to make sure it executes on the correct workbook.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Re: Two workbooks open, code changing wrong Wb, help!

    Thanks Norie, I tried that and get "Runtime error 9""Subscript out of Range".

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,097

    Re: Two workbooks open, code changing wrong Wb, help!

    You are using the sheet codename, but you can only use that directly for the workbook containing the code.
    So you need to use the actual sheet name as shown by Norie, if you are getting an RTE 9 then it means the sheet name is wrong.

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

    Re: Two workbooks open, code changing wrong Wb, help!

    You need to use the sheet tab names rather than codenames.

    So change Sheets("Sheet1") to Sheets(tabname) where tabname is the tab name of the sheet with the code name 'Sheet1'.

  6. #6
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Re: Two workbooks open, code changing wrong Wb, help!

    Quote Originally Posted by Norie View Post
    You need to use the sheet tab names rather than codenames.

    So change Sheets("Sheet1") to Sheets(tabname) where tabname is the tab name of the sheet with the code name 'Sheet1'.
    Odd, I tried that as well with same error.

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

    Re: Two workbooks open, code changing wrong Wb, help!

    Where are you getting the error?

    Have you checked the sheet names for leading/trailing spaces?

  8. #8
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Re: Two workbooks open, code changing wrong Wb, help!

    Good grief, I entered the tab names and did not think I put a space, but yes, there was a space!! Thanks for your help Norie.

+ 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] Date changing to wrong month in code
    By heathb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-05-2019, 04:25 AM
  2. [SOLVED] Workbooks.open number format wrong
    By Teddi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-18-2019, 08:15 AM
  3. [SOLVED] Using a Variable FileName in macro to open file but wrong code to close it
    By cljohnston64 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-19-2015, 01:15 PM
  4. [SOLVED] What is wrong with this code? (Loop through WBs in folder - open, edit, save & close)
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-04-2013, 07:49 PM
  5. [SOLVED] whats wrong with this code: need to open picture from folder in IE
    By Lkivagten in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-01-2012, 05:32 PM
  6. My code try to open other workbooks.
    By Cabeza2000 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-14-2007, 08:09 AM
  7. Excel passwords are changing when two workbooks are open.
    By Password in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 12-21-2005, 12:35 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