Checking for an open file in Excel

  • Thread starter Thread starter posheroff
  • Start date Start date
P

posheroff

I need to have a macro check to see if a certain file is
open, close it if it is, and go back to the original sheet
if it is not. How do I do that? Any help would be
appreciated.
 
Hi

what you could do is write a For Each Statement something like

For each wkb in Workbooks
If wkb.Name = "MyFile.xls" then
wkb.Close
Exit Sub
End If

HTH

Davi
 
Actually, I was hoping for some sort of an "If" statement
that if that file is not open, then it will return me to
my activeworksheet and allow me to continue, something
like:

If "myfile.xls" is open then
"myfile.xls".close
else
Activesheet.range (xx).select

I just can't seem to figure out how to write that to get
the desired result. I would really appreciate any
assistance.
 
Hi posheroff,

I would just try to close the workbook and ignore the error condition that
will be raised if it's not open:

On Error Resume Next
Workbooks("myfile.xls").Close SaveChanges:=False
On Error GoTo 0

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 

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

Back
Top