MrExcel Publishing
Your One Stop for Excel Tips & Solutions

If "File is open" then...

Posted by jssp on September 03, 2001 8:44 AM

Hello all:

Is it possible to ask a file to run a Macro only if
another file is open?

For example, tell Excel: only add this two numbers if the
file "Data.xls" is open, otherwise don't do it.

Thanks, as always, for all your help!!!

Posted by Dax on September 03, 2001 3:09 PM

How about a function like this? You could of course just include the code as part of your normal subroutine if you didn't want a seperate function.

Function IsDataxlsOpen() As Boolean
Dim wb As Workbook
For Each wb In Excel.Workbooks
If LCase(wb.Name) = "data.xls" Then IsDataxlsOpen = True: Exit Function
End Function


Posted by John on September 03, 2001 3:23 PM

On Error Resume Next
If Err = 0 Then
'your code here
End If
On Error GoTo 0

Posted by jssp on September 04, 2001 7:49 AM

Thanks Dax and John for your help and time;

I have not been able to make John's code work for
me yet; perhaps I'm missing something, but I get
your idea.

Dax's function works extremely well.

Thanks again for all your help..!!!