Go back to the previous excel file

H

houghi

I have the following macro that works great:
Sub OpenData()
ChDir "Y:\somewhere"
Workbooks.Open Filename:= _
"Y:\somehwere\Tables.xls"
Windows("calculation.xls").Activate
End Sub

This works as long as the macro is run in the file "calculation.xls".
How can I change it so that it will return to the file from which the
macro has been launched? I uderstand that I could change the name
"calculation.xls" each time for a new file, but I am sure that I and
others will sometimes forget and then have to go manually to that file.

Or is it possible to open the file "Y:\somehwere\Tables.xls" without it
going to the front?

houghi
 
G

Gary''s Student

Just store the caption of the active workbook and use it to re-Activate the
window:

Sub dural()
Dim w As Window
Set w = ActiveWindow
n = w.Caption
'ChDir.......
'Workbooks.Open Filename:=.....
Windows(n).Activate
End Sub
 

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