+ Reply to Thread
Results 1 to 4 of 4

Activate Worksheet doesn't work with two open workbooks

  1. #1
    Registered User
    Join Date
    02-14-2009
    Location
    Houston
    MS-Off Ver
    Excel 2007
    Posts
    8

    Activate Worksheet doesn't work with two open workbooks

    I have a program with two open workbooks -- one has data in it, the other has parameters for running the program. All code is in personal.xlsb.

    I have to switch between the two workbooks during the processing loop, but I can't activate the worksheet that's on the bottom. Here's the code that represents it, below is the results.

    Please Login or Register  to view this content.

    Here are the results:

    If the Parms sheet is open on top:

    set MyBook active Parms.xlsm RunParms
    Set PBook active Parms.xlsm RunParms

    If the Registered sheet is open on top:

    set MyBook active 50K Records.xlsm Registered
    Set PBook active 50K Records.xlsm Registered

    If I explicitly activate the sheets, as in the code below, I get a runtime error 9 subscript out of range on whichever sheet is underneath:


    Please Login or Register  to view this content.
    Thanks for your help.

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Activate Worksheet doesn't work with two open workbooks

    I'm not sure what you're trying to do but let's start with the variable declarations.
    Please Login or Register  to view this content.
    Only PSheet is declared as a worksheet which means only PSheet is explicitly a worksheet, MySheet is going to be a variant.
    There is a common mistaken belief that typing more than one variable and as "type" at the end makes them all that type, that is not the case.

    When you declare a workbook explicitly using a file name that is correct and that is how you should leave it, then you can activate that workbook using the variable name.
    Using set = activeworkbook means you are now pointing that variable to whatever the active workbook is, it's not activating the workbook.
    Last edited by skywriter; 07-15-2023 at 12:24 AM.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,542

    Re: Activate Worksheet doesn't work with two open workbooks

    Please Login or Register  to view this content.
    Maybe explain what you are trying to achieve.
    Experience trumps academics every day of the week and twice on Sunday.

  4. #4
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Activate Worksheet doesn't work with two open workbooks

    @bpatters - ActiveSheet & ActiveWorkbook are read-only properties of a "containing" object.
    To change the "Active" object, use the Activate method:
    Please Login or Register  to view this content.
    Last edited by leelnich; 07-15-2023 at 03:27 AM.
    Clicking the Add Reputation star below helpful posts is a great way to show your appreciation.
    Please mark your threads as SOLVED upon conclusion (Thread Tools above Post # 1). - Lee

+ 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. Replies: 5
    Last Post: 05-22-2023, 04:58 AM
  2. [SOLVED] Open statement doesn't work on protected workbooks
    By bullshot25 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-09-2016, 12:16 PM
  3. [SOLVED] Macro work in personal workbook, doesn't work in other workbooks
    By Centexcel in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-30-2013, 11:47 AM
  4. Replies: 2
    Last Post: 02-28-2013, 11:39 AM
  5. Activate the open workbook (that doesn't contain the code!)
    By seaveegee in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-19-2012, 10:23 AM
  6. Replies: 0
    Last Post: 07-05-2012, 02:16 AM
  7. Replies: 0
    Last Post: 07-04-2012, 07:27 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