Solved

Excel Modeless UserForm - visible only when the activeworkbook when launched has focus

Posted on 2013-05-17
2
706 Views
Last Modified: 2013-05-17
Logic for creation of a Modeless UserForm exists in a central XLAM that my users have access to.  They launch it from a button on a custom toolbar created by the XLAM.

Modeless is necessary since users will have this userform open to assist them with various interactions of their workbooks.  However, if the user switches to a different Excel workbook I would prefer that the userform no longer be visible (since it is only intended to be interacting with the specific workbook that was active at the time they launched the user form).

      1 - So userform logic lives in File (A) - an XLAM
      2 - User launches a separate XLSM file - File (B); and while (B) is active, they launch the user form from (A).
      3 - User uses Userform to assist with their interactions with File (B).
      4 - If user decides to switch focus to a different workbook - File (C), they should no longer be able to see or use the UserForm; since (B) is no longer the active workbook.

I'm ok with either hiding the user form while the currently active book (File C) does not equal the original book that was active at the time launched (File B); or with unloading the userform as soon as they leave the book that was active at the time launched (forcing them to relaunch it when they later come back to the original book and want to again use the functionality on the user form).

Does anyone have suggestions?  So far most of the examples I've been able to find have dealt with tieing the behavior into ThisWorkbook Deactivate/Activate but i don't see how that would work since the userform logic lives in a standalone book (File A) separate from the original workbook that was active (File B) when they launched the userform.  So I don't think This Workbook would work since the userForm code doesn't actually live in the file whose active status needs to be monitored.

Any guidance would be greatly appreciated!
Thanks
0
Comment
Question by:shellysuelll
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39175983
If you add a class module to your add-in, you can trap application-level events, such as Workbook_Activate or Workbook_Deactivate. You will need to save the name (in a static variable) of the workbook that was active when the userform is loaded. The Workbook_Deactivate sub can then hide or unload the userform, and the Workbook_Activate sub can restore it upon return to the original workbook.

Chip Pearson covers the basics here: http://www.cpearson.com/excel/AppEvent.aspx
0
 

Author Closing Comment

by:shellysuelll
ID: 39176477
Thanks - you are totally right - the class module trapping is what I needed.
You Rock!
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question