How to Find a Value Across Multiple Excel Files Using a VBA Macro
Question details
A master workbook needs to automatically search through multiple Excel files in a specific folder to find values listed in column A, returning the corresponding filename and cell reference for each match.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The user wants to automate a cross-workbook data extraction process by looping through an entire directory of files to locate specific data points.
- Observed behavior
- The goal is to implement a VBA script that records the matching filename and address into column B of the master file without manually opening each target workbook.
Ensure all the target Excel files you want to search are placed into a single dedicated folder. Your master workbook should be saved outside of this folder to prevent the macro from attempting to search the master file itself.
Create a VBA Macro to Loop Through the Target Folder
Use the Visual Basic Editor to write a macro that iterates over every file in your target folder, searches for the value, and writes the results back to the master sheet.
VBA (Visual Basic for Applications) allows you to automate repetitive tasks like opening multiple workbooks, searching for data, and recording results. By defining a folder path in your code, the macro can seamlessly open each file, search for the criteria in column A, log the exact filename and cell address in column B, and close the file.
Note that macros written in Microsoft Excel VBA may not run natively in alternative software like LibreOffice Calc, as LibreOffice uses a different scripting language.
Before writing any code, click 'File' > 'Save As' and ensure your master workbook is saved in the Excel Macro-Enabled Workbook (.xlsm) format. Standard .xlsx files cannot store macros.
Press the keyboard shortcut Alt + F11 to launch the Visual Basic Editor. Alternatively, navigate to the Developer tab on the ribbon and click 'Visual Basic'.
In the Visual Basic Editor, right-click your workbook name in the Project pane on the left, select 'Insert', and then choose 'Module'. This creates a blank canvas for your script.
Construct your VBA code to loop through your designated folder (e.g., a desktop folder named 'myFolder'). The code should iterate through each worksheet of each file, utilize the '.Find' method to locate the value from column A, and output the 'Workbook.Name' and 'Cell.Address' to column B.
Once the code is written, return to your Excel workbook. Press Alt + F8 to open the Macro dialog box, select the macro you just created, and click 'Run'.

Automate Cross-Workbook Searches with WPS Spreadsheets
WPS Office provides robust support for VBA macros, allowing you to run cross-workbook search scripts and automate complex data analysis just as you would in Microsoft Excel.
- 1. Open your Master Workbook in WPS: Launch WPS Spreadsheets and open your .xlsm file containing the search criteria.
- 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon and click 'VBA Editor' to access the coding environment.
- 3. Run the Search Macro: Press Alt + F8 to bring up the macro list, select your cross-workbook search macro, and execute it to find your values.

Frequently Asked Questions
Will this Excel VBA script work in LibreOffice Calc?
No, LibreOffice uses its own scripting language called LibreOffice Basic, which is not perfectly identical to Excel VBA. The code will likely throw errors and need to be rewritten to function in LibreOffice.
Why can't I save my macro in a standard Excel file?
Standard Excel files (.xlsx) are designed strictly for data and cannot store executable code for security reasons. You must use the Excel Macro-Enabled Workbook (.xlsm) format to save and run VBA scripts.
What happens if the master workbook is inside the target folder?
If the master workbook is located inside the same folder being scanned, the macro may attempt to open and search the master file itself, causing a recursive error or infinite loop. It is best practice to keep the master file outside the target directory.
How do I easily access my macros to run them?
You can quickly access all available macros in your workbook by pressing the Alt + F8 shortcut key. This opens the Macro dialog where you can select and run your desired script.




