Make most recently open workbook active window


Active Member
May 6, 2010
I am not quite sure how to ask this question b/c I am not sure of the correct terminology but here it goes. I have 2 workbooks each with their own closing procedures. Let's assume one workbook is open and I open the 2nd workbook from a macro in the 1st. How do I make the second book assume the activewindow position? Let me give an example of what I mean to better explain. If I have both workbooks open and I hit the red x in the upper right corner, it runs the before close procedures for the first workbook. So far I have created variables to bypass the before close sub from wb 1 but it is getting complicated. It occurred to me that perhaps there is some simple solution that makes the 2nd workbook the activewindow allowing its before close sub to run instead of wb 1's.

Help please.

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Tom Urtis

MrExcel MVP
Feb 10, 2002
The premise of your question is still not clear because you wrote this:

"Let's assume one workbook is open and I open the 2nd workbook from a macro in the 1st. How do I make the second book assume the activewindow position?"

Based on the literal interpretation of that, unless you have code to force activation otherwise in your Open procedure, at the moment you open another workbook, or add another workbook for that matter, said workbook *is* the active workbook.

So what is the problem? If as you say you are "hit[ing] the red x in the upper right corner" then that means you are closing all the workbooks. As long as they are all closing anyway, what difference does it make which one was active since they will both end up closed, with their (presumably) BeforeClose events running as you have coded them.


Active Member
May 6, 2010
I am new to VBA and am certain I am probably not using the correct language. Having said that, in each workbook I have made it so the user must use the exit buttons I have created. So when the user hits the red x it will give them a message telling them to use mine. Using mine forces a save under a different name as well as a few other procedures. The procedures differ between the 2 workbooks. So when the user hits the red x in the 2nd wb it gives the msgbox from the 1st workbook when I want them to see the message from the 2nd workbook.

There may be better ways of accomplishing what I want accomplished but with my limited VBA knowledge this is what I have created. Again, to this point, I have set variables that will bypass wb 1's closing event allowing wb 2's to take over.

I guess my question is why does it run wb 1's first and how can I make it run wb 2's instead without setting a variable?

It my be my limited knowledge not of VBA but of the general basics of windows that is holding me back.


Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...