Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel - If a file is open do run the code 2

Status
Not open for further replies.

zevw

MIS
Jul 3, 2001
697
US
I am using an auto_open macro

I guess it is very simple but I never ran such code in excel. I would like to check if a certain file is open then do not run the auto_open code and if the file is not open or it does not exist on this machine do not run the code.

Thanks in advance for you help!!
 
Hi there,

For checking if workbooks are open or not, I always use a function..

Code:
Function WbOpen(wbName as string) as boolean
    On Error Resume Next
    WbOpen = Len(Workbooks(wbname).name)
End Function

Then, in your workbook_open code (suggested over the auto_open) could look something like this ...

Code:
Private Sub Workbook_Open()
    If WbOpen("book1.xls") Then
        'do code here
    Else
        MsgBox "Workbook not open!", vbInformation, "ERROR!"
    End If
End Sub

Regards,
Zack Barresse

Simplicity is the ultimate sophistication. What is a MS MVP? PODA
- Leonardo da Vinci
 
Thanks for your code, it works like a snap!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top