Open file crash if it doesnt exist

buzz71023

Active Member
Joined
May 29, 2011
Messages
295
Office Version
  1. 2016
Platform
  1. Windows
I have the below code that works great as long as the file it is searching for exist.
I am running into an issue that if the file doesn't exist, Excel just crashes.
I need help only opening the file if it exist and if it down not then just exiting the sub.

VBA Code:
Dim MyFileDir As String, FName As String, myXLFile As String
Dim myCurrWB As Workbook, myCurrDate As Range, myCurrTime As Range, FirstDate As Range, FirstTime As Range

Set myCurrWB = ActiveWorkbook
Set myCurrDate = ActiveWorkbook.Sheets("Hours Sheet").Range("p2")
Set myCurrTime = ActiveWorkbook.Sheets("Hours Sheet").Range("w2")
Set FirstDate = ActiveWorkbook.Sheets("Hours Sheet").Range("A19")
Set FirstTime = ActiveWorkbook.Sheets("Hours Sheet").Range("C19")

FName = txtPrevJob.Text 'Change to suit your text boxes name
MyFileDir = "P:\Production\Hours Reporting Archives\" 'Change to suit
myXLFile = PDF_Dir & FName & "*"

Application.ScreenUpdating = False

Workbooks.OpenText Filename:=MyFileDir & myXLFile

globDate = ActiveWorkbook.Sheets("Hours Sheet").Range("P3").Value
globTime = ActiveWorkbook.Sheets("Hours Sheet").Range("W3").Value

ActiveWorkbook.Close False

myCurrDate = globDate
FirstDate = globDate
myCurrTime = globTime
FirstTime = globTime

Application.ScreenUpdating = True

ActiveWorkbook.Save

Unload Me
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
VBA Code:
Sub Testing()

On Error GoTo ErrorHandle

Dim MyFileDir As String, FName As String, myXLFile As String
Dim myCurrWB As Workbook, myCurrDate As Range, myCurrTime As Range, FirstDate As Range, FirstTime As Range

Set myCurrWB = ActiveWorkbook
Set myCurrDate = ActiveWorkbook.Sheets("Hours Sheet").Range("p2")
Set myCurrTime = ActiveWorkbook.Sheets("Hours Sheet").Range("w2")
Set FirstDate = ActiveWorkbook.Sheets("Hours Sheet").Range("A19")
Set FirstTime = ActiveWorkbook.Sheets("Hours Sheet").Range("C19")

FName = txtPrevJob.Text 'Change to suit your text boxes name
MyFileDir = "P:\Production\Hours Reporting Archives\" 'Change to suit
myXLFile = PDF_Dir & FName & "*"

Application.ScreenUpdating = False

Workbooks.OpenText Filename:=MyFileDir & myXLFile

globDate = ActiveWorkbook.Sheets("Hours Sheet").Range("P3").Value
globTime = ActiveWorkbook.Sheets("Hours Sheet").Range("W3").Value

ActiveWorkbook.Close False

myCurrDate = globDate
FirstDate = globDate
myCurrTime = globTime
FirstTime = globTime

Application.ScreenUpdating = True

ActiveWorkbook.Save

Unload Me

Exit Sub

ErrorHandle:
Application.ScreenUpdating = True
Msgbox "An Error has occurred. Perhaps Workbook Not Found."
End Sub

Looks like you may need Error Handling?

Is this what you're going for?
This will go to End Sub when there's an error.
 
Upvote 0

Forum statistics

Threads
1,215,156
Messages
6,123,339
Members
449,098
Latest member
thnirmitha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top