Sunday, April 10, 2022

Select and open an excel file using filedialog vba

 Dim wb As Workbook
  Dim wc, wd, oSheet As Worksheet
 
 ' Create and set the file dialog object.
    Dim fd As Office.FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    With fd
        .Filters.Clear
        .Title = "Select an Excel File"
        .Filters.Add "Excel Files", "*.xlsx?", 1
        .AllowMultiSelect = False
        .InitialFileName = "C:\Users\allso\Desktop\test"
        Dim sFile As String
    
        If .Show = True Then
            sFile = .SelectedItems(1)
        End If
    End With
    
    If sFile <> "" Then
       Set wb = Workbooks.Open(sFile)   ' Open the Excel file.
    End If

 'save and close the file

   wb.Close savechanges:=True

No comments:

Post a Comment