Excel

TheSmallman.com

an XL ideas Lab

Dashboards VBA
  • Home
  • Dashboards
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
    • Power Pivot a User Guide
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About
Menu

Excel Dashboards VBA

Street Address
City, State, Zip
Phone Number
an XL ideas Lab

Your Custom Text Here

Excel Dashboards VBA

  • Home
  • Dashboards
  • Excel Tips
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
  • PowerPivot
    • Power Pivot a User Guide
  • Courses
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About

Excel File Picker VBA

June 3, 2020 Marcus Small
Choose Folder VBA

Choosing a file with as an Excel file picker can be useful if you want to be able to pick a specific file to edit in a VBA procedure. The process is can be used when you want human interaction and there is no way to know the file name for the procedure in advance. In this case the person that runs the process will have a file picker open and they can choose the file they need to open during the VBA process.

The most critical part about the choose file VBA process is the file path your users are going to choose from. The path should be in existence. In the following example we have included the file path on the desktop.


Path = "C:\Users\Desktop\"  


This is declared with a constant however, if you wanted to declare the variable with a cell reference just change out this line with the following two lines.

Out


Const Path = "C:\Users\Desktop\"   'Change to suit


New Lines


Dim Path As String
Path = [A1].Value


Where cell A1 has the file path. When the procedure is run my desktop should open.

Open Folder VBA

The following is the VBA code to run the file picker procedure.

Option Explicit

Sub ChooseFile()
Const Path = "C:\Users\Desktop\"   'Change to suit
Dim fd As FileDialog
Dim fName As String 
Dim i As Integer

Set fd = Application.FileDialog(msoFileDialogFilePicker)

    fd.Title = "Select the XL File"
    fd.InitialFileName = Path
    i = fd.Show
    fd.Filters.Clear
    fd.Filters.Add "Excel files", "*.xls*"
         If i <> -1 Then
             MsgBox "You Cancelled, try again later"
         Else
            fName = Right$(fd.SelectedItems(1), Len(fd.SelectedItems(1)) - InStrRev(fd.SelectedItems(1), "\"))
        End If
    Set wb = Workbooks.Open(Path & fName)
End Sub


If the user chooses to cancel the VBA procedure part way through then there is a trap in the code to cover this possibility.  The code will stop and the user will be allowed to exit the VBA process.


This VBA procedure will allow you to have a dialog open in Excel and you can choose the file which will open. This type of procedure is perfect where the path is known but the file to choose is not. Hope this helps.

Tags excel, file, Dialog, picker, open, directory
← Upload Data with Power QueryConnect Slicer to Multiple Data Sources →

Featured Posts

Excel Dashboards: Tracking a Crisis

Excel Dashboards: Tracking a Crisis
April 14, 2020

Recent Posts

Populating an Excel Table from a Range of Cells with VBA

Populating an Excel Table from a Range of Cells with VBA June 12, 2025

Fuzzy Distribution with Randbetween

Fuzzy Distribution with Randbetween May 21, 2025

Add Minimum and Maximum for Chart in Cells

Add Minimum and Maximum for Chart in Cells March 12, 2025

Inflation Over Multiple Years in a Single Cell

Inflation Over Multiple Years in a Single Cell January 10, 2025

Hubspot Dashboard

Hubspot Dashboard October 3, 2024

Monthly Dashboard With Supporting Metrics

Monthly Dashboard With Supporting Metrics September 25, 2024

Excel Show Missing Sheet Tabs

Excel Show Missing Sheet Tabs July 29, 2024

Run Macro Overnight Automatically

Run Macro Overnight Automatically June 24, 2024

Split File into Parts and Save to Directory

Split File into Parts and Save to Directory April 20, 2024

Most Popular Author

Most Popular Author December 14, 2023

 

Follow US:

 
 

MarcusSmall@thesmallman.com

 

TheSmallman.com - Making your small systems hum...
© Copyright 2013-2024 theSmallman.com All Rights Reserved.