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 Read Outlook Emails Reading emails from Outlook and capture them in Excel file is very common activity being performed in office environment. Doing this activity manually every time is quite boring and…
This Excel VBA Code helps to Get User Name. Here is an example environ(username) or Application.username.This macro gets the username from active directory.
Video: How to Hide Worksheet in Excel? Hide Sheet in Excel When I was creating an excel dashboard, there were multiple sheets which I used for calculation purpose and never wanted anybody to make any…
How to send bulk emails from outlook using excel VBA? Have you ever felt the need of an Excel based VBA tool or code which can help you to draft Outlook emails in bulk by…
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
VBA Code to send Outlook Emails Sending bulk emails is a very common activity, there are many office activities that need a person to send bulk emails to single or multiple recipients. You also may…
One Comment