How to Automatically Retrieve Data from Password-Protected Excel Files
Question details
The user needs to automate the process of extracting data from several password-protected workbooks into a central master workbook using VBA.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Consolidating data from multiple secured files without having to manually open each one and type the password.
- Observed behavior
- Manual password prompts halt the automation process, requiring a scripted solution to pass credentials programmatically while acknowledging security risks.
Back up all your source workbooks and the master file before running new macros. Be aware that hardcoding passwords in VBA creates a security risk, so you should restrict access to the master workbook and password-protect the VBA project itself.
Use a VBA Macro to Open, Copy, and Close Protected Files
Automate data extraction by storing file paths and passwords in a VBA dictionary, allowing the script to bypass security prompts and pull the needed data seamlessly.
By utilizing the Scripting.Dictionary object in VBA, you can map multiple file paths to their respective passwords. The macro iterates through these files, opens them by supplying the password programmatically, copies the defined range to your master workbook, and closes the source files without saving changes.
Press Alt + F11 in Excel to launch the Microsoft Visual Basic for Applications editor.
In the Project Explorer pane on the left, right-click your master workbook name, select Insert, and then click Module.
Initialize a Scripting.Dictionary object to store your files. Add entries using passwords.Add "C:\Path\To\File1.xlsx", "password1". Use a For Each loop to iterate through the dictionary keys.
Inside the loop, use Set wbSource = Workbooks.Open(Filename:=filePath, Password:=passwords(filePath)) to open the file. Define your source range (e.g., Range("A1:B10")) and copy its values to the next empty row in your destination worksheet.
Add wbSource.Close SaveChanges:=False to ensure the protected file is closed immediately after extraction without prompting to save.
Close the VBA editor, return to your master workbook, press Alt + F8, select your new macro, and click Run to execute the data retrieval.

Automate Data Processing with WPS Spreadsheet
WPS Office provides excellent built-in support for VBA macros, allowing you to run complex automation scripts—including opening password-protected files—just like in Microsoft Excel.
- 1. Install WPS Office: Download and install the free WPS Office suite on your computer.
- 2. Open Your Master Workbook: Launch WPS Spreadsheet and open the master file where you want to collect the data.
- 3. Access the Macro Editor: Go to the Tools tab on the ribbon and click Macro, or press Alt + F11 to open the VBA Editor.
- 4. Run Your Script: Paste your data retrieval script into a module and run it to automatically gather data from your protected files.

Frequently Asked Questions
What happens if a password in the VBA code is wrong?
If you implement basic error handling like 'On Error Resume Next' before the 'Workbooks.Open' command, the macro will skip the file with the incorrect password, optionally display an error message if programmed to do so, and proceed to extract data from the next file in the list.
Can I retrieve data from a protected workbook without opening it?
No, if an Excel workbook is encrypted with a password, it cannot be read via standard background queries (like ADO or Power Query) without decrypting it first. The VBA script must temporarily open the file in memory to access the data.
How do I hide the files opening and closing during the macro execution?
You can hide the visual process and speed up the macro by adding 'Application.ScreenUpdating = False' at the beginning of your VBA script, and 'Application.ScreenUpdating = True' at the very end.




