+ Reply to Thread
Results 1 to 11 of 11

Userform loses focus after activating another workbook

  1. #1
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Userform loses focus after activating another workbook

    Hi all,

    I believe this is a Excel 2013 onwards issue with workbooks now opening on different windows. I've been trying a few ways to try and resolve this to no avail.

    Basically this, I have a userform, which upon clicking, activates a separate workbook (which is already open). That workbook, upon activation, sits in front of the userform and the userform loses focus. I can't do anything on the userform until I click on the old workbook to reactivate.

    Is there a way to keep userform in view with the background switching between the two workbooks?

    I attach here a simple workbook to showcase this example. In it lies a button which creates a new workbook and shows a userform. The userform have 2 buttons. To showcase this issue, click on the one that says "Click Me!" which will switch workbook and lose focus of the userform. The other button closes the userform.

    Macro that opens the userform (Module1)
    Please Login or Register  to view this content.
    Code in Userform1
    Please Login or Register  to view this content.
    Any thoughts / advice / help is much appreciated.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Userform loses focus after activating another workbook

    workbooks now opening on different windows...
    The change from an MDI interface in XL 2010 to an SDI interface in 2013 makes this a bit of a problem.

    Simplistically, an MDI (Multiple Document Interface) interface has all workbooks in the same 'physical' window but in an SDI (Single Document Interface) each workbook appears in its own window, but is still the same instance of Excel. Regardless if it's the same instance, the new Window is treated as a separate application so the userform stays 'docked' to the original window.

    There's no real fix, only some workarounds and they all involve the use of APIs (AFAIK) to set the parent window of the userform to the active workbook - not sure of this is something you want to get into so info only to start with.

  3. #3
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: Userform loses focus after activating another workbook

    Yeah, I figured as much. That's pretty annoying.

    Quote Originally Posted by cytop View Post
    There's no real fix, only some workarounds and they all involve the use of APIs (AFAIK) to set the parent window of the userform to the active workbook - not sure of this is something you want to get into so info only to start with.
    If you have a solution for this I would very much like to know. I tried something to try and keep the userform windows in front but I couldn't get it to work and causes it to crash from time to time.

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Userform loses focus after activating another workbook

    Your sample file reworked...

    The main changes is some API declarations in the userform and a reference to the Excel application what uses WithEvents to trap the Window_Activate event which is used to set the parent of the userform to the active window.
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: Userform loses focus after activating another workbook

    Thanks cytop! That works brilliantly. I'll try to make it work over the main project I was working on.

    Do you have a good source of information for all these Windows API stuff?

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Userform loses focus after activating another workbook

    ...information for all these Windows API stuff
    To put the 'All' in context - there are thousands of API functions; everything from Beeping the speaker to encrypting a string so that the NSA can't break it...

    I'll have a look later at some introductory pages for APIs, but to give a flavour of what you can do...
    Add Minimise/Maximise buttons to a userform
    Position mouse over a command button (Post #7 but the others give context)
    Create Windows Pop-up menu at cursor position on Right Mouse click

    (All those link off this site)

    There are simpler things, the most common is to fin a window (or control in a window) - you can click a button in another application, for example, or send text to other application.

    Initial reading would be the FindWindow, FindWindowEx, SendMessage & PostMessage API calls. (They can do a lot ) See this for an example of those.
    Last edited by cytop; 04-24-2017 at 05:58 AM.

  7. #7
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: Userform loses focus after activating another workbook

    Thanks cytop! Much appreciated!

  8. #8
    Registered User
    Join Date
    12-23-2019
    Location
    NJ, USA
    MS-Off Ver
    365 Business
    Posts
    7

    Re: Userform loses focus after activating another workbook

    I know that this thread is a few years old, but I could really use some help with the same issue.
    The solution provided by cytop seems to be just what I need, but I'm having difficulty integrating it into my code.

    My macro creates 6 new workbooks, then copies data from multiple sheets of ThisWorkbook and pastes over to Sheet1 of the newly created workbooks and saves them.

    Example:
    ThisWorkbook has 6 sheets: A, B, C, D, E, F
    The code first adds 6 new workbooks, each one corresponding to one of the above 6 sheets.
    A given range is copied from ThisWorkbook sheet A.
    Copied data is pasted to Sheet1 of Workbook A.
    Workbook A is saved.
    The same is repeated sequentially for each of the other new workbooks (B-F).
    Upon completion a message box informs the user that process is complete.

    This procedure can take a quite a few seconds to complete, so I added a UserForm to display on screen throughout the duration of the processing.
    I did this by simply adding "UserFormName.Show vbModeless" at the start of the subroutine. Then I have this at the end, just before the message box: "Unload UserFormName".
    The UserForm ShowModal property is set to FALSE.

    The problem is that the UserForm is immediately covered up by the added workbooks.

    How can I keep the UserForm at the front during the entire procedure?

  9. #9
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,939

    Re: Userform loses focus after activating another workbook

    Quote Originally Posted by DD990 View Post
    I know that this thread is a few years old, but I could really use some help with the same issue......

    How can I keep the UserForm at the front during the entire procedure?

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  10. #10
    Registered User
    Join Date
    12-23-2019
    Location
    NJ, USA
    MS-Off Ver
    365 Business
    Posts
    7

    Re: Userform loses focus after activating another workbook

    Quote Originally Posted by FDibbins View Post
    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    Okay, will do. Thank you.

    Most other forums that I've used advise to search for a thread about a particular topic to see if it already exists before creating a new post.

    Is it permissible for me to use the exact title of this post for my new post? Because it is literally the same topic. I'm unsure how else to describe it.
    I'm also concerned that doing so might be in violation of Forum rule #5.

  11. #11
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,939

    Re: Userform loses focus after activating another workbook

    yes, you can. And, as I said, you can reference this in your own thread, if needed

+ 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] Form loses focus after using Outlook folder picker
    By Tooley in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-13-2017, 01:39 PM
  2. Textbox loses focus?
    By mike0123m in forum Excel General
    Replies: 1
    Last Post: 02-28-2012, 06:44 PM
  3. activating userform without showing workbook
    By dan2010 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-24-2010, 03:52 AM
  4. VB code error when workbook loses focus
    By Protiusmime in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-12-2008, 12:43 AM
  5. [SOLVED] Repost:Excel not active / loses focus.Pls help!
    By michael.beckinsale in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-02-2006, 03:45 AM
  6. [SOLVED] I'm at a real loss here. Excel loses focus!!!!!!
    By michael.beckinsale in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-28-2006, 11:25 AM
  7. Excel crashes when Combo Box loses focus
    By ExcelEddie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-17-2006, 03:55 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