open last active workbook

craigexcel

Active Member
Joined
Jun 28, 2006
Messages
298
Office Version
  1. 2016
Platform
  1. Windows
I have created a 'utility' workbook that contains a number of macros that coworkers & I use for various processes. Since each of the processes to be completed is always done on a workbook that a user opens, I'd like to automate always returning to the previously 'active' workbook. I've been searching for a while for a solution but have yet to come up with one.

I tried both of these in the "This Workbook" module's Auto_Open event, without success, and in fact, it didn't look like anything happened. (1) Application.SendKeys ("%{TAB}"), and (2) some version of "... .ActivatePrevious" (but I already deleted that one, instead of just commenting it out, since it didn't work, and I don't remember what the rest of it was).

So, my question is: does VBA code exist that will, once the 'utility' workbook has been opened, the user is immediately returned to the (still open) workbook that was just the 'active' workbook?

I'm using Office 365 (i.e. Excel 2016).
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Put this code in the ThisWorkbook Module of the 'utility' workbook :

Code:
Option Explicit

Private WithEvents app As Application
Private oPrevWb As Workbook

Private Sub Workbook_Open()
    Set app = Application
End Sub

Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
    If Wb.Name = "[COLOR=#ff0000][B]Enter here the name of the workbook the user opens.[/B][/COLOR]" Then oPrevWb.Activate
End Sub

Private Sub app_WorkbookActivate(ByVal Wb As Workbook)
    Set oPrevWb = Wb
End Sub
 
Last edited:
Upvote 0
Thanks Jaafar for your reply. The text you wrote, that's shaded red, would not be for a specific workbook though. The user could open any 1 of many workbooks that the macros in the 'utility' workbook can be used on. That's why I wanted to activate the 'previous' (still open) workbook, regardless of its name. Apologies if I didn't make that clear. Any other thoughts on how to accomplish this with this added information?
 
Upvote 0
Thanks Jaafar for your reply. The text you wrote, that's shaded red, would not be for a specific workbook though. The user could open any 1 of many workbooks that the macros in the 'utility' workbook can be used on. That's why I wanted to activate the 'previous' (still open) workbook, regardless of its name. Apologies if I didn't make that clear. Any other thoughts on how to accomplish this with this added information?

Just remove that If clause line but it will activate the previous active workbook every time a workbook is opened during the session regardless of the workbook that was just opened:

Code:
Option Explicit

Private WithEvents app As Application
Private oPrevWb As Workbook

Private Sub Workbook_Open()
    Set app = Application
End Sub

Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
    oPrevWb.Activate
End Sub

Private Sub app_WorkbookActivate(ByVal Wb As Workbook)
    Set oPrevWb = Wb
End Sub
 
Last edited:
Upvote 0
Thanks again Jaafar. I tried your revised code, but it gets stuck on the "oPrevWb.Activate" line. I'm missing something, since I can't see what "oPrevWb" has been defined as. Can you help?
 
Upvote 0
Thanks again Jaafar. I tried your revised code, but it gets stuck on the "oPrevWb.Activate" line. I'm missing something, since I can't see what "oPrevWb" has been defined as. Can you help?

Yes, you are right .. I forgot to set the oPrevWb var when opening the 'utility' workbook !

Try this :
Code:
Option Explicit

Private WithEvents app As Application
Private oPrevWb As Workbook

Private Sub Workbook_Open()
    Set app = Application
    Set oPrevWb = Me
End Sub

Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
    oPrevWb.Activate
End Sub

Private Sub app_WorkbookActivate(ByVal Wb As Workbook)
    Set oPrevWb = Wb
End Sub
 
Last edited:
Upvote 0
I just tried the revised code, and still no luck. The 'utility' workbook remained the active workbook after it opened, and the code didn't switch to the previous (still open) workbook, which had been the active workbook. Any other ideas? Also, as a side question, what does "Me" mean in the context of the line "Set oPrevWb = Me"?
 
Upvote 0
Ok - Sorry I misunderstood you . So you want to activate the previous active workbook when the 'utility' workbook is opened - Right ?
If so, you may need to place the code in an addin or the personal workbook
 
Upvote 0
Looking at your code again, I'm wondering about the "Set oPrevWb = Me" line. Since that's in the Workbook_Open section, wouldn't that set "oPrevWb" as the 'utility' workbook, since that's the workbook the code is running from?

Code:
Private Sub Workbook_Open()
    Set app = Application
    [COLOR=#ff0000]Set oPrevWb = Me[/COLOR]
End Sub
 
Last edited:
Upvote 0
Looking at your code again, I'm wondering about the "Set oPrevWb = Me" line. Since that's in the Workbook_Open section, wouldn't that set "oPrevWb" as the 'utility' workbook, since that's the workbook the code is running from?

Code:
Private Sub Workbook_Open()
    Set app = Application
    [COLOR=#ff0000]Set oPrevWb = Me[/COLOR]
End Sub

Yes that's correct but I got the logic wrong because I had misunderstood your question ... I'll post the correct code, hopefully, in a moment but you will have to place the code in an Addin
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top