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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
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
 
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
 
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
 
Back
Top