VBA Code to Browse a Folder

Complete Excel VBA Course

Quite often a VBA developer requires code to browse a folder. This is mainly for saving the output file or reading the input file(s). Below is the VBA code that can be used to browse a file folder.

VBA Code to Browse a Folder
Public Sub PickAFolder()
    'Microsoft Office XX.X Object Library is required to run this code
    'Variable declaration
    Dim objFileDialog As FileDialog
    Dim objSelectedFolder As Variant
    'Browse a folder
    Set objFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With objFileDialog
        .ButtonName = "Select"
        .Title = "Select a folder"
        .InitialView = msoFileDialogViewList
        For Each objSelectedFolder In .SelectedItems
            'Show the selected folder details on Excel sheet
            Sheet1.Range("B6").Value = objSelectedFolder
    End With
End Sub

VBA Code to Browse a Folder Steps:

It is worth to mention that you need to add Office reference (Microsoft Office XX.X Object Library) in Excel VBA from Menu Bar (Tools>References…). Below are the steps to add Office reference in Excel VBA:

1. From the Menu Bar, click on Tools > References…

Complete Excel VBA Course
VBA Code to Browse a Folder

2. Select ‘Microsoft Office XX.X Object Library and click on the ‘OK’ button

VBA Code to Browse a Folder

To use this code in your Excel file, follow below steps:

1. Open an Excel file
2. Press Alt+F11
3. Insert a Module (Insert>Module) from menu bar
4. Paste the code in the module
5. Now add a shape in Excel sheet
6. Give a name to the shape like ‘Browse a Folder’

7. Right click on the shape and select ‘Assign Macro…’

8. Select PickAFolder from the list and click on ‘Ok’ button

9. Done

Download Practice File

You can also practice this through our practice files. Click on the below link to download the practice file.

Recommended Articles

Excel VBA Course : Beginners to Advanced

We are offering Excel VBA Course for Beginners to Experts at discounted prices. The courses includes On Demand Videos, Practice Assignments, Q&A Support from our Experts. Also after successfully completion of the certification, will share the success with Certificate of Completion

This course is going to help you to excel your skills in Excel VBA with our real time case studies.

Lets get connected and start learning now. Click here to Enroll.

Secrets of Excel Data Visualization: Beginners to Advanced Course

Here is another best rated Excel Charts and Graph Course from ExcelSirJi. This courses also includes On Demand Videos, Practice Assignments, Q&A Support from our Experts.

This Course will enable you to become Excel Data Visualization Expert as it consists many charts preparation method which you will not find over the internet.

So Enroll now to become expert in Excel Data Visualization. Click here to Enroll.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *