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.
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
.Show
For Each objSelectedFolder In .SelectedItems
'Show the selected folder details on Excel sheet
Sheet1.Range("B6").Value = objSelectedFolder
Next
End With
End Sub
2. Select ‘Microsoft Office XX.X Object Library and click on the ‘OK’ button
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
VBA Code To Change Cell Color Excel supports more than 16 million colors in a cell; hence you should know how to set the exact color in a cell. To do this, you can use…
How to Find Duplicate Files In excel using VBA? Yesterday I was working on my computer and cleaning the drives to make some more space. I was surprised to see so many files saved at…
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…
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.
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 Excel.
Table of Content VBA Code to Get User Domain Name VBA Code to Get User Domain Name – Method 1 VBA Code to Get User Domain Name – Method 2 Steps to use this VBA…