Error handling on Workbooks.Open

T

Tom

Workbooks.Open ("H:\Checklist 07-23-07.xls")
Workbooks("CheckList.xls").Activate

What would be the best way to handle the error if the file does not exist?
 
J

Joel

filename = Dir(("H:\Checklist 07-23-07.xls")
if filename = <> then
'enter errror code
else
Workbooks.Open ("H:\Checklist 07-23-07.xls")
end if
 
J

JE McGimpsey

The "best way" will depend on what you want to happen if the file
doesn't exist (e.g., do you want to exit the sub silently? Put up an
error message? invoke a dialog to find the file?).

One of the simplest:

Dim wkbk As Workbook
On Error Resume Next
Set wkbk = Workbooks.Open("H:\Checklist 07-23-07.xls")
On Error GoTo 0
If wkbk Is Nothing Then Exit 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