Open File - Need an warning to appear if file cannnot be found

B

Brite

I was wondering how i would go about adding a warning message that says "File
cannot be found" to the following code if the the test.xls file cannot be
found?

Sub filefile
Workbooks.Open FileName:="C:\test\test.xls"
MyVal = Application.Workbooks("test.xls").Sheets("Sheet1").Range("A1").Value
Workbooks("test.xls").Close SaveChanges:=False
End Sub

thanks in advance for your help!
 
M

Mike H

one way:-

Sub filefile()
On Error GoTo errorroutine
Workbooks.Open Filename:="C:\test.xls"
MyVal = Application.Workbooks("test.xls").Sheets("Sheet1").Range("A1").Value
Workbooks("test.xls").Close SaveChanges:=False
Exit Sub
errorroutine:
msg = "File not found"
MsgBox msg
End Sub

Mike
 
D

David G

one way:-

Sub filefile()
On Error GoTo errorroutine
Workbooks.Open Filename:="C:\test.xls"
MyVal = Application.Workbooks("test.xls").Sheets("Sheet1").Range("A1").Value
Workbooks("test.xls").Close SaveChanges:=False
Exit Sub
errorroutine:
msg = "File not found"
MsgBox msg
End Sub

Mike

You should probably reset On Error after the file has been opened.
Don't want false errors coming up.
Add "On Error Goto 0" after the Workbooks.Open line.

David G
 
B

Brite

You guys are the best! Thanks!

David G said:
You should probably reset On Error after the file has been opened.
Don't want false errors coming up.
Add "On Error Goto 0" after the Workbooks.Open line.

David G
 

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