How to Fix VBA Error 1004 When Opening a Selected File in Excel
Question details
The user needs to resolve an issue where Excel VBA fails to open a file chosen via FilePicker.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Using an Excel VBA macro to prompt the user to select a file using FilePicker, and then attempting to open that selected file using the Workbooks.Open method.
- Observed behavior
- Excel throws Runtime Error 1004 indicating the file cannot be accessed, which typically happens when the file path string is invalid or improperly formatted.
Open your VBA editor and use the Immediate Window (Ctrl + G) to verify the exact string output of your selected file path before running the open command.
Remove Extra Backslashes from the Selected File Path
Fix the path formatting by using the exact string returned by the FilePicker without appending unnecessary directory separators.
When using the FilePicker dialog in VBA, the `.SelectedItems(1)` property returns the complete, absolute file path (including the file name and extension). Appending a backslash to this string invalidates the path format, which causes Excel to trigger Error 1004 when `Workbooks.Open` attempts to read it.
Press ALT + F11 in Excel to open the Visual Basic for Applications Editor, and locate the module containing your macro.
Find the line where you assign the selected file to a variable. It should look similar to `sFile = .SelectedItems(1)`.
Check the subsequent lines for any code that adds a backslash (e.g., `sFile = sFile & "\"`). Delete or comment out this line to keep the original path intact.
Ensure your file opening command uses the unmodified variable directly, like this: `Workbooks.Open(sFile)`. Save the macro and run it again.

Write and Run VBA Macros in WPS Office
WPS Office provides highly compatible VBA environment for your spreadsheet automation. You can write, edit, and run your macros, including FilePicker operations, directly within WPS Spreadsheet without worrying about compatibility issues.
- 1. Install WPS Office: Download and install the latest version of WPS Office on your computer.
- 2. Open Your Macro-Enabled File: Launch WPS Spreadsheet and open your existing .xlsm file.
- 3. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon menu.
- 4. Open the VBA Editor: Click the 'Macros' button or press ALT + F11 to open the VBA Editor, modify your file paths, and execute your code smoothly.

Frequently Asked Questions
Why does FilePicker return a full path instead of just the folder?
The `msoFileDialogFilePicker` dialog is designed specifically for selecting individual files. Therefore, it returns the absolute path including the file name and extension. If you need just the directory path to append file names manually later, use `msoFileDialogFolderPicker` instead.
What are other common causes for VBA Error 1004?
Error 1004 is a generic application-defined error in Excel VBA. Aside from invalid file paths, it frequently occurs if you attempt to open a file that is already open, if you lack read permissions for the target folder, or if the macro references a worksheet range that does not exist.
How can I verify if a file exists before opening it in VBA?
You can use the built-in `Dir()` function to validate the path. Adding a check like `If Dir(sFile) <> "" Then Workbooks.Open(sFile)` ensures your macro only attempts to open the file if the path is perfectly valid and the file actually exists on the drive.




