ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Cant find workbook (https://www.excelbanter.com/excel-programming/286281-cant-find-workbook.html)

Todd Huttenstine[_2_]

Cant find workbook
 
The below code opens the workbook in Textbox6.
Workbooks.Open TextBox6.Value

If the workbook is not located, I get a VBA error, instead
I would like a messagebox to popup and say "Workbook not
found." and then exit sub.

Thanx

Todd Huttenstine

Chris Putzig

Cant find workbook
 
Try this:

On Error Resume Next 'disable error
myBook =TextBox6.Value
Set testOpened = Workbooks.Open(myBook)
If Not IsObject(testOpened) Then
MsgBox myBook & " not found"
End If
On Error GoTo 0 'reset error

....Chris

"Todd Huttenstine" wrote in message
...
The below code opens the workbook in Textbox6.
Workbooks.Open TextBox6.Value

If the workbook is not located, I get a VBA error, instead
I would like a messagebox to popup and say "Workbook not
found." and then exit sub.

Thanx

Todd Huttenstine




Jim Rech

Cant find workbook
 
Here are a couple ways to trap the error:

Sub Way1()
If Dir(TextBox6.Value) < "" Then
Workbooks.Open TextBox6.Value
Else
MsgBox "File cannot be found"
Exit Sub
End If
''Continues....
End Sub

Sub Way2()
On Error Resume Next
Workbooks.Open TextBox6.Value
If Err.Number < 0 Then
MsgBox "Error opening workbook. The specific" & _
"Excel error message was " & Err.Description
Exit Sub
End If
''Continues....
End Sub


--
Jim Rech
Excel MVP



Rob van Gelder[_4_]

Cant find workbook
 
On Error GoTo e
Workbooks.Open TextBox6.Value

Exit Sub
e: MsgBox "Workbook not found."


"Todd Huttenstine" wrote in message
...
The below code opens the workbook in Textbox6.
Workbooks.Open TextBox6.Value

If the workbook is not located, I get a VBA error, instead
I would like a messagebox to popup and say "Workbook not
found." and then exit sub.

Thanx

Todd Huttenstine





All times are GMT +1. The time now is 11:49 PM.

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