Vba folder dialog in Excel

Filedialog is used to pick a file of any kind during the execution of the program.

This provides a file dialog box functionality similar to the functionality of the standard Open and Save dialog boxes found in Microsoft Office applications.

The�FileDialog�property is located in each individual Office application's�Application�object.

There are four types of Filedialog object:

1.Open

2.Save As

3.FilePicker

4.Folder Picker

In this chapter, File picker is explained.

As the name indicates, Filepicker is the one that allows the user to choose a file during the course of a program.

This situation arises in a number of occassions where a user has t select a file to work on.

In these times, the file picker is used.This displays a dialog box opting the user to choose a file of choice.

The follwing example illustrates this:

  1. Sub filedilog()
  2. Dim fDialog As FileDialog, result As Integer
  3. Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
  4. 'Optional: FileDialog properties
  5. fDialog.AllowMultiSelect = False
  6. fDialog.Title = "Select a file"
  7. fDialog.InitialFileName = "C:\"
  8. 'Optional: Add filters
  9. fDialog.Filters.Clear
  10. fDialog.Filters.Add "Excel files", "*.xlsx"
  11. fDialog.Filters.Add "All files", "*.*"
  12. 'Show the dialog. -1 means success!
  13. If fDialog.Show = -1 Then
  14. MsgBox fDialog.SelectedItems(1)
  15. End If
  16. End Sub

The above code opts the user to select a file and dispaye the same to the user in a messagebox.

The screenshot of the code and the output is as follows

excel vba folder dialog

excel vba folder dialog

excel vba folder dialog

 

You can find similar Excel Questions and Answer hereunder

1) What are pivot tables. Here an introduction to understand them in Excel

2) How to convert a number or string into a data with the cdate function

3) How can I remove Outline from my WorkSheet?

4) Vba list all files in a folder in Excel

5) How can I get users to select a file for processing using my macro?

6) How to rename multiple sheets easily with VBA

7) I have entered lot of comments in my WorkSheet - how do I have them show up when printed?

8) How can I get users to select a folder to save the output of my macro?

9) How can I filter a table to get all records that have less than a particular value in a specific column?

10) I have test scores for my student population. In Excel, how can I determine confidence interval for my Population Mean?

 

Here the previous and next chapter