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:
Sub open_file()
Workbooks.Open "C:\Users\Dell\Desktop\myFile.xlsx"
End Sub
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"
.Show
For Each objSelectedFile In .SelectedItems
Range("E2").Value = objSelectedFile 'You may change the destination as per your requirement
Next
End With
End Sub
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:
Thats how you can browse excel file using VBA Code.
To help you practice this code, we have made this code available through practice file. Click on the below link to download the practice file.
Random Rows Selector is an MS Excel based tool which can be used to pick random or stratified samples from a set of records available in the Excel. The tool is fully dynamic, it can support any data format in Excel.
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
Did you come across any requirement where you want the user to interact with a sheet only through VBA Form? Here is a simple code which can help you.
VBA Code to Browse a Folder 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…
Colorindex in Excel VBA Today let’s try to understand how ColorIndex property in Excel VBA works. It is an easy and effective way to quickly complete the development. ColorIndex property is normally used by VBA…
VBA Code To Add Items In Listbox Control Using ListBox in Userform is very common. You can use ListBox.AddItem function to add items in the listbox.; however, it is little difficult to add items in…
One Comment