Saturday, January 4, 2014

VBA How to open a file from Input from user via "browse"

The following codes will open a window and then you can go to the desired folder to select the file:

Sub GetFile()
Dim fNameAndPath As Variant, wb As Workbook
fNameAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLSX), *.XLS", Title:="Select File To Be Opened")
If fNameAndPath = False Then Exit Sub
Set wb = Workbooks.Open(fNameAndPath)
'
'do stuff
'
wb.Close savechanges:=True 'or false
End Sub

No comments:

Post a Comment