Go to Last Active Window

S

Steven

I have a macro that opens a file and then goes to that file making it the
active window. I then want to go back to the previous file but not tuse the
name of the file because the file it goes back to can be any file (ie it will
not always be the same file.) How do you programmatically go to the last
window open without using the file name.

Thank you,

Steven
 
P

Peter T

If #I follow what you mean, include something like the following in your
macro

Dim wb as Workbook

Set wb = Activeworkbook ' maintain a ref' to the original workbook

' workbooks.open filename

' make the orignal workbook active
wb.Activate

Regards,
Peter T
 
J

Jim Thomlinson

So just to be clear you have 3 files to deal with. The one that is running
the code. Another one that is active and finally a new one that you are
opening/selecting?
 
G

Gary''s Student

It depends on how you "go to" that file. Say we have Book1 and Book2 open
with Book1 being active. This macro:

Sub gothere()
Application.Goto
Reference:=Workbooks("Book2").Worksheets("Sheet1").Range("A154")
End Sub

will "goto" a specific place in Book2. Now lets say we want to return, but
have not remembered where we came from. We just want to go back. All we
need to do is:

Sub goback()
Application.Goto
End Sub

Excel keeps a stack of the places we visit if we have a chain of "gotos".

Application.Goto
without an argument is like the back arrow in IE.
 
S

Steven

And we have a winner. Thank you very much.

Peter T said:
If #I follow what you mean, include something like the following in your
macro

Dim wb as Workbook

Set wb = Activeworkbook ' maintain a ref' to the original workbook

' workbooks.open filename

' make the orignal workbook active
wb.Activate

Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top