none
FileDialog RRS feed

  • Question

  • 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


    • Edited by Lopkest Friday, July 15, 2016 3:23 PM
    Friday, July 15, 2016 3:22 PM

Answers

  • Use this as the first line:

    If Dir("C:\ExampleFolder\" & ThisWorkbook.Worksheets("Sheet1").Range("A1").Value) = "" Then Exit Sub

    • Marked as answer by Lopkest Friday, July 15, 2016 4:54 PM
    Friday, July 15, 2016 3:36 PM