open last active workbook

craigexcel

Active Member
Joined
Jun 28, 2006
Messages
295
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).
 
... have tried the code posted by GTO. In short, it kind of worked. When opening the 'utility' workbook, it remains as the active workbook, instead of the intended previous active workbook...

If I have one or more other workbooks open, the window to the one with focus becomes active after opening the 'utility' wb. I have tested opening the utility wb thru Excel and thru Win Explorer. I am using Excel 2010, but cannot imagine this to be the issue.

Do you have ANY other code included in Workbook_Open or Auto_Open? If yes, please post it.

I will be logging out for the day shortly, but am confident Jaafar or another member will spot what is reactivating the utility wb.

Mark
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Thanks Mark. No, thee is no other code included in the Workbook_Open, which is in the "This Workbook". Only what you had posted previously. There are a number of other macros within the 'utility' workbook, but all of those are in their own Modules. The only potential concern that comes to mind, and only because I'm not familiar with them at all (I copied from other posts I found for what I was trying to do), is the following. (I only included the "Dim ..." part.) Would any of these possibly cause an issue with the "Workbook_Open" process?
Code:
Dim objFSO      As FileSystemObject
Dim objFolder   As Object
Dim objFile     As Object
 
Upvote 0
Try introducing a brief delay before running the macro and see if it works for you :
Code:
Private Sub Workbook_Open()
  If Workbooks.Count > 1 Then
     Application.OnTime Now, Me.CodeName & ".ActivateWb"
  End If
End Sub

Private Sub ActivateWb()
    ActiveWindow.ActivateNext
End Sub
 
Last edited:
Upvote 0
Eureka! (Well, you found it! :)) That solved the issue. THANK YOU, Jaafar! And of course, I'll ask: why did that solve the issue? Would like to learn why this fixed it, so I can handle (or avoid) it in the future.
Great Voltaire quote, too!
 
Last edited:
Upvote 0
Eureka! (Well, you found it! :)) That solved the issue. THANK YOU, Jaafar! And of course, I'll ask: why did that solve the issue? Would like to learn why this fixed it, so I can handle (or avoid) it in the future.
Great Voltaire quote, too!

Glad you had this working in the end :)

Based on my experience, sometimes, for various reasons, a vba command is not actually executed until the initial calling routine exits .. The Workbook_Open event routine is one of those routines because sometimes, the workbook takes too long to be loaded and thus ties up excel.
 
Last edited:
Upvote 0
Glad you had this working in the end :)

Based on my experience, sometimes, for various reasons, a vba command is not actually executed until the initial calling routine exits .. The Workbook_Open event routine is one of those routines because sometimes, the workbook takes too long to be loaded and thus ties up excel.
Thanks again, Jaafar! That must (may??) be related to the fact that this is a network document, and the delay in loading could well be attributable to that.
 
Upvote 0
When you work with Winword, you can access the most recent document by adding "/mfile1" after the name of the exe. Is there a similar solution for Excel that doesn't include VBA-code?

Bengt
 
Upvote 0

Forum statistics

Threads
1,215,359
Messages
6,124,488
Members
449,165
Latest member
ChipDude83

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