Checking for an open file in Excel

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.
 
D

DSC

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
 
P

posheroff

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.
 
J

Jake Marx

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]
 
Top