ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Open File - Need an warning to appear if file cannnot be found (https://www.excelbanter.com/excel-programming/387582-open-file-need-warning-appear-if-file-cannnot-found.html)

Brite

Open File - Need an warning to appear if file cannnot be found
 
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!

Mike H

Open File - Need an warning to appear if file cannnot be found
 
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

"Brite" wrote:

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!


David G[_4_]

Open File - Need an warning to appear if file cannnot be found
 
On Apr 17, 12:26 pm, Mike H wrote:
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


Brite

Open File - Need an warning to appear if file cannnot be found
 
You guys are the best! Thanks!

"David G" wrote:

On Apr 17, 12:26 pm, Mike H wrote:
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




All times are GMT +1. The time now is 09:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com