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.
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