Hello,
My program has to search a file name that is written inside the cell A1. What I want to do is that if there is no file with that name, exit sub instead of opening a FileDialog with nothing in it (or open to close automatic). Here is what my code looks like:
Sub Filepicker ()
Dim fd as FileDialog
Dim FileName as String
'open filedialog application
set fd = Application.FileDialog(msoFileDialogFilePicker)
'the filename is written in A1
FileName = ThisWorkbook.Worksheets("Sheet1").Range("A1")
With fd
.InitialFileName = "C:\ExampleFolder\" 'Startup folder
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "", FileName & ".pdf"
'Here is my problem
If .SelectedItems.Count <> 0 then
MsgBox ("There is no file with that name")
Exit Sub
Else
.Show
If False Then
Exit Sub
End If
End If
End With
'Rest of Code
End Sub