VBA Code to Browse a File

There are few VBA codes which are commonly used by every developer. One of them is giving an option to user to browse a file. Below is a sample code where I have given a browse button (shape) in an Excel sheet. User can click on this button/shape and select an Excel file. The browsed file path is then displayed in cell E2:

VBA code to browse a file

Public Sub BrowseAFile()
    Dim objFileDialog As Object
    Dim objSelectedFile As Variant
    'Browse the file
    Set objFileDialog = Application.FileDialog(3)
    With objFileDialog
        .ButtonName = "Select"
        .AllowMultiSelect = False
        .Filters.Clear 'It is important to clear old filters before adding new one
        .Filters.Add "Excel File", "*.xls;*.xlsx;*.xlsm", 1   'You may add more filters and give them a sequence
        .Title = "Select Input file"
        For Each objSelectedFile In .SelectedItems
            Range("E2").Value = objSelectedFile 'You may change the destination as per your requirement
    End With
End Sub

Below is the description of the available options in the code to customize the code for your requirement

FileDialog Options

ButtonName: Name of the select button, I have used ‘Select’ in the code
AllowMultiSelect: To allow user to select one or multiple files, I have allowed only one input file in the code
Filters: To show only specific type of files to user for selection (Example: .Filters.Add “Word File”, “*.doc;*.docx”, 1 to select a word file)

It is worth to mention that if you want to allow user to select multiple files then you need to make few changes in the code else it will overwrite the path mentioned in cell E2 instead of creating a list

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

  1. Open the Excel file where you want to count the color cells
  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 file’
  7. Right click on the shape and select ‘Assign Macro…’
    Assign Macro
  8. Select BrowseAFile from the list and click on ‘Ok’ button
    Assign Macro Dialog
  9. Done

Leave a Reply

VBA Code to Sum Cells by Color
VBA Code to Sum Cells by Color

Recently some of our subscribers have requested us to share a VBA code that can sum the cells by its color. To help our subscribers and developers, we are sharing 2 codes that be used to sum the cells with specific color and returns the total sum of the matching color cells

VBA Tool to Track Start and End Time (Time & Motion Tracker)

Here is one more wonderful free tool from ExcelSirJi.com which makes your life easy. Time & Motion Tracker helps you to track Start and End time of any type of transaction or activity. The good thing is, it is VBA based tool which helps you to protect from manual manipulation in the data by the user. It is also easy to use, just click on Start (shortcut: Ctrl+Shift+A) or Stop (Ctrl+Shft+S) buttons to record the time

VBA Code to Delete File
VBA Code to Delete File

In this article we are going to show you how you can delete file or files using a single line of VBA code. 1. Delete a specific file from the folder, 2. Delete specific type of files from the folder, 3. Delete all files from the folder


Like the Article? Please spread the word :)