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.
VBA CODE TO HIDE MENU RIBBON IN MS ACCESS In MS Access, there are multiple ways to protect your code or tool from un-authorized access and edits. Some developers prefers to hide MS Access ribbons…
Excel VBA Tool To Get File Properties Here is one more interesting VBA tool from the ExcelSirJi team. File Properties Tool is an Excel VBA tool that gets the following properties of the file. File…
In this article we will learn about VBA code to get computer name. Excel VBA, or Visual Basic for Applications, is a programming language that can be used to automate tasks within the Microsoft Excel…
VBA to Read Excel Data Using Connection String Sometimes as a programmer you need to read heavy (more then 5 MB) Excel files. There are two ways you can read data from Excel files: Open…
File Manager tool is an Excel based tool which helps you to delete or move unwanted files from your system. It requires a source and destination folder (in case you want to move files). First it lists all the files available in the folder or sub-folders then you can select the action to be taken for each file such as Move or Delete. With a click of button, tool will take all necessary actions.
VBA code that will sum cells by its color through excel function. This code will really help in making the analysis and presentation better.
One Comment