VBA & Macro Problems

How to Automatically Retrieve Data from Password-Protected Excel Files

Tauseeq MagsiTauseeq Magsi Nov 10, 2026 869 views

Question details

The user needs to automate the process of extracting data from several password-protected workbooks into a central master workbook using VBA.

How to Automatically Retrieve Data from Password-Protected Excel Files
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 in Excel to launch the Microsoft Visual Basic for Applications editor.

2
Insert a New Module

In the Project Explorer pane on the left, right-click your master workbook name, select Insert, and then click Module.

3
Write the Automation Script

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.

4
Open and Copy Data

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.

5
Close the Source File

Add wbSource.Close SaveChanges:=False to ensure the protected file is closed immediately after extraction without prompting to save.

6
Run the Macro

Close the VBA editor, return to your master workbook, press Alt + F8, select your new macro, and click Run to execute the data retrieval.

Use a VBA Macro to Open, Copy, and Close Protected Files
Securing Your Code: To prevent others from viewing the plain-text passwords in your script, go to Tools > VBAProject Properties in the VBA editor, click the Protection tab, check 'Lock project for viewing', and set a strong password.

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. 1. Install WPS Office: Download and install the free WPS Office suite on your computer.
  2. 2. Open Your Master Workbook: Launch WPS Spreadsheet and open the master file where you want to collect the data.
  3. 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. 4. Run Your Script: Paste your data retrieval script into a module and run it to automatically gather data from your protected files.
Fully compatible with Microsoft Excel macro-enabled formats (.xlsm, .xlsx).Integrated VBA editor for writing, editing, and executing scripts seamlessly.Free and lightweight alternative for heavy data consolidation tasks.Robust document encryption to protect your master workbooks.
QA img-9

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.