Skip to content

Instantly share code, notes, and snippets.

@Se7enSquared
Created December 22, 2021 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Se7enSquared/716150a777fc4b1711798e61389ede68 to your computer and use it in GitHub Desktop.
Save Se7enSquared/716150a777fc4b1711798e61389ede68 to your computer and use it in GitHub Desktop.
' ----------------------------------------------------------------
' Function: ChooseExcelFile
' Purpose: Get the location of the chosen excel file from dialog
' Params: String: starting_path: The folder to open the dialog in
' example: Environ$("USERPROFILE") & "\Downloads" or
' example2: C:\Data (no ending \)
' Returns: String: The full path to the chosen excel file as string
' By: HG
' Date: 12/21/2021
' ----------------------------------------------------------------
Private Function OpenExcelFile(starting_path As String) As String
Dim fd As Office.FileDialog
Dim fileName As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
On Error GoTo nofilechosen
With fd
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsx?", 1
.Title = "Choose an Excel file"
.AllowMultiSelect = False
.InitialFileName = starting_path
If .Show = True Then
fileName = fd.SelectedItems(1)
OpenExcelFile = fileName
End If
End With
Exit Function
nofilechosen:
MsgBox "No file was selected. Please try again."
End
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment