Hi guys,
the below macro opens a list of workbooks in a specific file directory... however when it gets to the last file, it opens it but crashes....
specifically crashes at If Dir(Cel.Value) = "" Then ' If File doesn't exist ...
any ideas how i can rectify this?
the below macro opens a list of workbooks in a specific file directory... however when it gets to the last file, it opens it but crashes....
VBA Code:
Sub CheckFileExistsV2_5()
'
Dim Cel As Range
Dim FileExistsErrorString As String
Dim FileExistsValidString As String
'
FileExistsErrorString = "Files that did not open:" & vbCrLf ' Initiate string that saves files that didn't open
FileExistsValidString = "Files that opened successfully:" & vbCrLf ' Initiate string that saves files that opened
'
For Each Cel In Range("Q2:Q" & Range("Q" & Rows.Count).End(xlUp).Row) ' For every cell in the range of files to be opened ...
If Dir(Cel.Value) = "" Then ' If File doesn't exist ...
MsgBox "All the workbooks are now open" ' Append the FileName to the FileExistsErrorString
Else ' If File exists ...
Workbooks.Open Cel.Value ' Open the file
FileExistsValidString = FileExistsValidString & Cel.Value & vbCrLf ' Append the FileName to the FileExistsValidString
End If
CheckNext:
Next
'
MsgBox FileExistsValidString & vbCrLf & vbCrLf & FileExistsErrorString ' Display message box of files that opened and didn't open
'
ThisWorkbook.Sheets("Main Menu").Activate ' Return to This workbook
Exit Sub
'
ErrorHandler:
On Error GoTo -1 ' Clear the error flag
On Error GoTo ErrorHandler ' Reset error handler
FileExistsErrorString = FileExistsErrorString & Cel.Value & vbCrLf ' Append the FileName to the FileExistsErrorString
GoTo CheckNext ' Return to code
End Sub
specifically crashes at If Dir(Cel.Value) = "" Then ' If File doesn't exist ...
any ideas how i can rectify this?