2

I am trying to open an excel file in a folder of excel files using VBA. I direct my code to take the end user straight to the folder and allow him to choose the file from the dialog box. But I am not able to open the file even after selecting it from the dialog box.

My understanding of the problem is - I am missing out on the Command to open the file after selecting it.

Here is my Code,

thisYear = Year(Date)


'change the display name of the open file dialog
    Application.FileDialog(msoFileDialogOpen).Title = _
    "Select Input Report"

 'Remove all other filters
 Application.FileDialog(msoFileDialogOpen).Filters.Clear

 'Add a custom filter
 Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _
     "Excel Files Only", "*.xls*")

     'Select the start folder
     Application.FileDialog(msoFileDialogOpen _
     ).InitialFileName = "\\driveA\Reports\" & thisYear & ""

Please kindly share your thoughts. Thanks.

4
  • 2
    This will get you started Commented Apr 25, 2017 at 21:24
  • @Kyle I am able to select the file from the dialog box but still the file doesn't get opened. I wonder there is a separate command or a line of code to open the file that I am selecting from the dialog box. Commented Apr 25, 2017 at 21:29
  • Did you read the link and look at the related example? It shows you how to extract the path of the selected file, and then you can simply use Workbooks.Open() with that path. Commented Apr 25, 2017 at 21:32
  • @Kyle Thanks for the help Commented Apr 25, 2017 at 21:36

1 Answer 1

2

I assume that you only let selecting one file (i.e. AllowMultiSelect = False).

 Dim file As String
 Dim myWbk As Workbook     

 file = Application.FileDialog(msoFileDialogOpen).SelectedItems(1) 

 Set myWbk = Workbooks.Open(file)

First line gets the path of the selected file and then second line opens it. Add this to the end of your code.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, Thanks @Masoud. I was looking to open a single file only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.