I'm using an Excel 2013 macro to extract data from a user-selected workbook and my vba is a bit rusty.
Application.GetOpenFilename
prompts the user for a file location, opens the file and returns a string. Workbooks.Open(string)
returns a Workbook - if you know the name in advance.
I want to combine these to ask the user which file to open, and return a Workbook.
Based on Frank's answer here (Open a workbook using FileDialog and manipulate it in Excel VBA) I've tried this:
Function openDataFile() As Workbook
'
Dim wb As Workbook
Dim filename As String
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
fd.Title = "Select the file to extract data"
'filename = fd.SelectedItems(1)
Set wb = Workbooks.Open(fd.SelectedItems(1))
openDataFile = wb
End Function
but this falls over on the commented line with Run-time error '5': Invalid procedure call or argument.
How do I prompt the user to open an excel file, and return a reference to it as a workbook?