+ Reply to Thread
Results 1 to 12 of 12

vba excel switch workbooks problem

  1. #1
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    vba excel switch workbooks problem

    A routine within ThisWorkbook opens a specific workbook and a routine runs from that workbook. In order for the routine to work correctly, ThisWorkbook has to be the ActiveWorkbook. Even if I set my workbooks properly, ThisWorkbook never activates after opening of the second workbook. Can someone tell me what I'm missing? Below is code within a ThisWorkbook module. ImportModules is a subroutine within the ActiveWorkbook module.
    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: vba excel switch workbooks problem

    afaik/ when you run the routine from that workbook, it becomes the ThisWorkbook. you would first have to select the original workbook first and run some code from it before it reverts back to thisworkbook.

    edit: must have read this wrong.
    Last edited by dmcgov; 06-13-2019 at 02:53 PM.

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: vba excel switch workbooks problem

    It is POSSIBLE that the "other" workbook is slow to come up, and that the wb.activate is running too early. You could try adding a short delay between opening the workbook and activating "This" workbook:

    Please Login or Register  to view this content.
    5 seconds is probably longer than needed, but I'll let you test that by reducing if 5 works.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

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

    Re: vba excel switch workbooks problem

    Another possible problem, do you have any code in the other workbook that triggers on opening?

  5. #5
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: vba excel switch workbooks problem

    Arkadi: that doesn't seem to remedy
    Fluff13: yes, I do as a matter of fact. Below is the code for the Wb2 (or ActiveWorkbook)
    Please Login or Register  to view this content.

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

    Re: vba excel switch workbooks problem

    The line you have flagged is indeed the problem.
    Change the name of the variable in one of the codes.

  7. #7
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: vba excel switch workbooks problem

    So I redim'd all wb references within Wb2 (ActiveWorkbook), but it still does not activate the wb (ThisWorkbook). Strangely when I insert a break on line Set swb = Workbooks.Open(FileName:=sPath, password:="password") and step through, ThisWorkbook does become the active workbook, but in runtime, ThisWorkbook is not activated. The timer doesn't seem to make a difference on activation or not.

    Background: I'm trying to implement Ron de Bruin's method of updating module and form code using another workbook that hosts the updating macro code. I know I could create this process as an add-in but I'm not quite educated and don't have the time to learn how to do this and this was the workaround I came up with for the time-being.

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

    Re: vba excel switch workbooks problem

    What if you make this change
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: vba excel switch workbooks problem

    Arrghh. I'm sorry - that doesn't work either.

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

    Re: vba excel switch workbooks problem

    What does the msgbox say
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: vba excel switch workbooks problem

    It works! The msgbox tells me it is ThisWorkbook. Even using wb.Name works. Finally.

    I initially had tried the msg "wtf", but I guess a more program friendly approach seems to have corrected the trajectory. Still curious as to why the operation is so buggy. Thank you!

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

    Re: vba excel switch workbooks problem

    Glad it's working & thanks for the feedback

+ 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. Excel for resources planning, problem with year switch
    By Pulleritz in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-08-2017, 11:50 PM
  2. [SOLVED] Cant switch between Workbooks any more
    By frank35 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-29-2013, 02:06 PM
  3. [SOLVED] Open two workbooks into one excel program, then macro code for button to switch between
    By oldboots in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-14-2013, 09:30 AM
  4. Replies: 0
    Last Post: 06-22-2006, 10:45 AM
  5. Replies: 0
    Last Post: 06-22-2006, 10:45 AM
  6. [SOLVED] How to switch between different workbooks?
    By Ivan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-28-2005, 02:35 AM
  7. URGENT: switch excel workbooks and display msgbox
    By ilyaskazi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-04-2005, 07: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