How to Use a Selected File Path as a VBA Macro Variable in Excel
Question details
The user wants to know how to capture a file path selected by a user via a VBA dialog prompt and store it in a variable for subsequent macro operations.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Writing a VBA macro that requires the user to select an external file, and needing to reference that exact file path dynamically later in the code.
- Observed behavior
- Needs a method to properly capture and store the file path from the file-selection dialog before performing actions like opening, copying, or modifying the file.
Ensure your macro settings allow the execution of VBA scripts and that you have saved your workbook as a Macro-Enabled Workbook (.xlsm).
Use Application.GetOpenFilename to Store the File Path
This is the most reliable method for storing a file path without immediately opening the file, making it ideal for flexible variable assignment.
While Application.FindFile physically opens the file and returns a simple True/False, Application.GetOpenFilename prompts the user with the standard file dialog and returns the actual string of the file path. This is the recommended approach for storing a file path as a variable.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor. Right-click on your workbook in the Project Explorer, choose 'Insert', and select 'Module'.
Create a new sub-procedure and declare a Variant variable to hold the path. For example: Dim selectedFilePath As Variant
Assign the dialog output to your variable using the code: selectedFilePath = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*")
Add an If statement to ensure the user didn't click Cancel. Use: If selectedFilePath <> False Then ... Inside this block, use your 'selectedFilePath' variable for tasks like Workbooks.Open(selectedFilePath).

Use FileDialog(msoFileDialogFilePicker) for Advanced Scenarios
Use the FileDialog object if you need more customization, such as allowing multi-selection or setting a specific starting directory.
Run Your VBA Macros Seamlessly in WPS Office
WPS Office offers robust built-in support for VBA macros, allowing you to execute scripts, handle file dialogs, and automate tasks just like in Microsoft Excel, but completely free.
- 1. Download and Install: Get the free version of WPS Office from the official website and install it on your PC.
- 2. Enable Developer Tools: Open WPS Spreadsheet, navigate to the settings, and ensure the Developer tab is enabled to access the macro tools.
- 3. Write and Run VBA Code: Press ALT + F11 to open the WPS Macro Editor, paste your file path selection script, and run it natively without missing a beat.

Frequently Asked Questions
Why does Application.FindFile return True or False instead of the file path?
Application.FindFile is designed to immediately open the file the user selects. It returns a boolean value (True if the file opened successfully, False if the user canceled). It does not return the file path string. To get the string without immediately opening the file, use Application.GetOpenFilename.
How do I handle users clicking 'Cancel' in the file dialog?
When using Application.GetOpenFilename, if the user cancels the prompt, the method returns the boolean value False. You should declare your path variable as a Variant and add a conditional check like 'If selectedFilePath = False Then Exit Sub' to handle cancellations gracefully.
Can I capture multiple selected file paths at once?
Yes. If you use Application.GetOpenFilename, you can add the argument MultiSelect:=True. This will return an array of file paths instead of a single variable, allowing you to loop through them using a For...Next loop.




