Open a Specific Workbook Defined by the UserExcel VBA Tutorial with GetOpenFilename

Let your users choose which Excel file to open — with a single, clean VBA macro using the GetOpenFilename method. No hardcoded paths, no friction, just a familiar file dialog your users already know how to use.
ACA | FMVA® | 19 Years in Finance
Why GetOpenFilename Changes Everything
Ever wanted to give your users the freedom to choose which workbook they want to open? Maybe you need a flexible template that can handle data from different sources, or you simply want to make your macros more versatile.
The solution is simpler than you might think. Excel VBA provides a built-in method called GetOpenFilename that displays the standard file open dialog — exactly what users see when they click File → Open in Excel. But unlike the basic approach, this gives you complete control over which files to show and what happens after the user makes their selection.
🎯Why This Matters
Hardcoding file paths into macros is one of the most common beginner mistakes in Excel VBA. When someone moves a file or runs your macro on a different machine, everything breaks. GetOpenFilename eliminates this problem entirely — the user always points to the right file themselves.
The Complete VBA Code
Here's the complete macro — clean, commented, and ready to use:
Sub OpenWorkbook()
Dim FName As Variant
' Step 1: Declare a variable to hold the file path
' Step 2: GetOpenFilename activates the standard dialog box
FName = Application.GetOpenFilename( _
FileFilter:="Excel Workbooks,*.xl*", _
Title:="Choose a Workbook to Open", _
MultiSelect:=False)
' Step 3: If a file was chosen (user didn't cancel), open it
If FName <> False Then
Workbooks.Open Filename:=FName
End If
End Sub⚠️ Save as Macro-Enabled Format First
Before pasting any VBA code, save your workbook as .xlsm (Excel Macro-Enabled Workbook) or .xlsb. A standard .xlsx file will silently strip your macros every time you save.
How It Works — Step by Step
The macro does three things in sequence. Each line has a specific job:
Declare the variable
Dim FName As Variant — we use Variant (not String) because if the user clicks Cancel, GetOpenFilename returns the boolean False. A String variable would throw a type mismatch error in that case.
Open the file dialog
Application.GetOpenFilename triggers the familiar Windows Open dialog. The FileFilter limits what the user sees to Excel files only. Crucially, this method does not open the file — it just returns the path the user selects.
Open the file (if selected)
The If FName <> False check handles the Cancel case gracefully. If the user chose a file, Workbooks.Open uses that path to open it. If they cancelled, the macro exits cleanly — no errors, no fuss.
Customizing the Dialog Box
The GetOpenFilename method is highly flexible. Here are the most useful customizations:
Multiple File Type Filters
Give users a dropdown of file types — Excel files, CSVs, or any combination:
FName = Application.GetOpenFilename( _
FileFilter:="Excel Files,*.xl*," & _
"CSV Files,*.csv," & _
"All Files,*.*", _
Title:="Select Your Data File")Allow Multiple File Selections
Set MultiSelect:=True and the user can select several files at once. The return value becomes an array rather than a single string:
FName = Application.GetOpenFilename( _
FileFilter:="Excel Workbooks,*.xl*", _
Title:="Choose Workbooks to Process", _
MultiSelect:=True)
' FName is now an array of file paths
If IsArray(FName) Then
Dim file As Variant
For Each file In FName
Workbooks.Open Filename:=file
Next file
End If💡Pro Tip
When using MultiSelect:=True, always validate with IsArray(FName) — not just FName <> False. When the user cancels, FName is still False (not an empty array), so the False check alone won't protect you from a runtime error.
Practical Applications
This technique unlocks several powerful use cases that come up constantly in financial and data work:
- Data Import Templates: Let users select their source data file — your template automatically imports, cleans, and processes it without any hardcoded paths.
- Report Generators:Users pick which workbook contains the month's data, and your report populates itself automatically.
- Consolidation Tools: Select multiple files to combine into one master workbook — essential for month-end consolidations across departments.
- Audit & Comparison Tools: Let users choose any two workbooks to compare for differences in values, formulas, or structure.
Step-by-Step Setup Instructions
Never added VBA to a workbook before? Follow these five steps exactly:
Save as Macro-Enabled Format
Go to File → Save As and choose Excel Macro-Enabled Workbook (.xlsm). This is required — a regular .xlsx file cannot store macros.
Open the VBA Editor
Press ALT + F11 on Windows or Option + F11 on Mac. Or go to the Developer tab and click Visual Basic.
Insert a New Module
In the VBA Editor, click Insert → Module. This creates a fresh code module where your macro will live. Do not paste code directly into the Sheet or ThisWorkbook objects.
Paste the Code
Copy the macro from Section 2 above and paste it into the module. Save the file with CTRL + S after pasting.
Run the Macro
Three ways to run it:
- Press ALT + F8, select
OpenWorkbook, click Run - Add a button to your worksheet and assign the macro to it
- Add it to the Quick Access Toolbar for one-click access
Troubleshooting Common Errors
Runtime Errors When Opening
If the macro fails after the user selects a file, check these common causes:
- File not found: The selected file was moved or deleted between selection and opening
- Permission denied: The file is already open in another program, or is read-only
- Format not supported: The selected file isn't a recognized Excel format
⚠️ Mac Users: Security Settings
On Mac, Excel may block macros by default. Go to Excel → Preferences → Securityand set Macro Settings to "Enable all macros", or use the Trust Center to add your file location as a trusted location.
Frequently Asked Questions
Does GetOpenFilename actually open the file?
No — that's the key insight. GetOpenFilename only shows the dialog and returns the path. It's Workbooks.Open that actually opens the file. This separation gives you full control over what happens before and after the user picks a file.
What happens if the user clicks Cancel?
GetOpenFilename returns the boolean value False (not a string). That's why FName is declared as Variant and checked with If FName <> False before attempting to open anything. Click Cancel = macro exits cleanly, no errors.
Can I filter for specific file types in the dialog?
Yes — the FileFilter parameter controls this. Use "Excel Workbooks,*.xl*" for all Excel formats, "CSV Files,*.csv" for CSVs, or chain multiple filters separated by commas to give users a dropdown of type options.
Can users select multiple files at once?
Yes, set MultiSelect:=True. The return value becomes an array of file paths. Loop through it with For Each to open each file. Always use IsArray(FName) to validate the selection rather than just checking for False.
Do I need to save as .xlsm?
Yes. Standard .xlsx files cannot contain macros. Excel will warn you when saving — choose Keep Macro-Enabled (.xlsm) to preserve your code. The .xlsb (binary) format also works and produces smaller files.
See It in Action
Download the working example workbook with the macro already set up and ready to run.
Prashant Panchal is a Chartered Accountant (ACA) and Financial Modelling & Valuation Analyst (FMVA®) with 19 years of experience in finance, FP&A, and financial modelling across the GCC region. He is the founder of FinDataPro.
