Skip to content
Microsoft OfficeAdvanced

How to Search Data Across Multiple Excel Files, Folders and Subfolders Using a VBA Macro – Search XLSX, XLSM, XLS, XLSB and CSV Files with Multiple Criteria

When data is stored across dozens, hundreds, or even thousands of Microsoft Excel files, finding a particular customer, invoice number, mobile number, GSTIN,...

BI
Bison Technical Team Enterprise IT specialists
Updated 20 Aug 2026 18 min read 0 total views

When data is stored across dozens, hundreds, or even thousands of Microsoft Excel files, finding a particular customer, invoice number, mobile number, GSTIN, email address, product code, employee name, transaction reference, or other value can become extremely time-consuming.

The problem becomes more complicated when the Excel files are distributed across a folder structure such as:

Advertisement
Customer Data
│
├── Delhi
│   ├── 2025
│   │   ├── Customers.xlsx
│   │   └── Sales.xlsx
│   │
│   └── 2026
│       └── Customers.xlsm
│
├── Mumbai
│   ├── Accounts.xls
│   └── Customers.xlsx
│
├── Punjab
│   └── Dealer Data.xlsb
│
└── Exports
    └── CustomerExport.csv

Opening every workbook manually and pressing Ctrl + F is inefficient and increases the possibility of overlooking important information.

A practical solution is to create a VBA-based Excel search utility that allows the user to select one main folder and automatically searches Excel files contained in that folder as well as its subfolders.

Such a macro can be designed to search .xlsx, .xlsm, .xlsb, .xls, and .csv files, search multiple worksheets, use up to three conditions, and consolidate matching records into a single result worksheet.


Why Excel's Normal Find Feature Is Not Enough

Excel's built-in Find command is excellent when the required information is already inside the workbook currently being used.

For example:

Ctrl + F → Enter Customer Name → Find All

works well for a single workbook.

However, the situation is very different when information is distributed among hundreds of files.

Suppose a company has:

  • 25 folders
  • 150 subfolders
  • 800 Excel files
  • Multiple worksheets in each workbook
  • Thousands of records in every file

The user may not even know which workbook contains the required information.

A recursive VBA search tool solves this problem by treating the selected folder structure as one searchable data source.


Main Objective of the Excel Search Macro

The objective is to create a centralized search facility with the following workflow:

Select Main Folder
        ↓
Find Excel/CSV Files
        ↓
Search Main Folder
        ↓
Search All Subfolders
        ↓
Open Each Workbook Read-Only
        ↓
Search Every Relevant Worksheet
        ↓
Apply Search Criteria
        ↓
Collect Matching Rows
        ↓
Display Results in One Worksheet

The user therefore does not have to know which individual Excel file contains the required record.


1. Select Any Folder for Searching

One of the most important improvements is allowing the user to select the folder dynamically.

Instead of hard-coding a path such as:

D:\Customer Data\

the macro can display the standard Windows/Excel folder selection dialog.

For example:

Please Select Folder

D:\Accounts Data\

After the folder is selected, the macro uses it as the starting point for the search.

This makes the same search workbook usable for different datasets without changing VBA code every time.


2. Automatically Search All Subfolders

Searching only the selected folder is often insufficient.

Consider:

D:\Customer Data\

with subfolders:

D:\Customer Data\Delhi\
D:\Customer Data\Delhi\2025\
D:\Customer Data\Delhi\2026\
D:\Customer Data\Mumbai\
D:\Customer Data\Mumbai\Old\
D:\Customer Data\Punjab\

A recursive search routine can automatically enter each subfolder.

Therefore, selecting only:

D:\Customer Data\

can be sufficient.

The user does not need to select Delhi, Mumbai, Punjab, 2025, 2026, and other folders separately.

This technique is generally known as recursive directory traversal.


3. Search Different Excel File Formats

A good search utility should not be restricted to one Excel format.

It can be configured to process common spreadsheet formats including:

XLSX

.xlsx is the standard modern Excel workbook format.

XLSM

.xlsm is a macro-enabled Excel workbook.

XLS

.xls is the older Excel 97–2003 workbook format.

XLSB

.xlsb is the Excel Binary Workbook format.

CSV

.csv is a comma-separated text format frequently generated by accounting software, CRM systems, ERP applications, portals, and database exports.

Therefore, one search operation can potentially inspect:

Customer.xlsx
Accounts.xls
Database.xlsm
Archive.xlsb
Export.csv

without requiring the user to perform separate searches.


4. Search All Worksheets Inside a Workbook

An Excel workbook may contain many worksheets.

For example:

Customers.xlsx

Sheet1
Customers
Dealers
Inactive
2025
2026

Searching only the first worksheet could miss important information.

A properly designed macro loops through the workbook's worksheets and checks each relevant sheet.

Conceptually:

Open Workbook

→ Search Sheet1
→ Search Customers
→ Search Dealers
→ Search Inactive
→ Search 2025
→ Search 2026

Close Workbook

This substantially increases the completeness of the search.


5. Search a Specific Column

Sometimes the user knows exactly where the information should exist.

For example:

Column Information
A Customer ID
B Customer Name
C Company
D Mobile
E Email
F GSTIN

If the user wants to search only the Mobile field, the search can be restricted to Column D.

The macro can accept different methods of identifying the column.

For example:

D

or:

4

or:

Mobile

This is useful because different users may be more comfortable with column letters, numbers, or header names.


6. Search by Header Name

Searching by the actual field heading is often easier than remembering Excel column numbers.

For example, the user can enter:

GSTIN

instead of:

F

Other examples include:

Customer Name
Company Name
Mobile
Email
Invoice Number
PAN
GSTIN
City
State
Product

The VBA routine checks the header row to determine the corresponding column.

This can make the search tool considerably easier for non-technical users.


7. ANY Column Search

Sometimes the user knows the value but does not know which field contains it.

For example, the user wants to search:

9876543210

but does not know whether it is stored under:

Mobile
Phone
Contact
Alternate Number
WhatsApp

In this situation, an ANY option can search across multiple columns in every record.

For example:

Search Field: ANY
Search Value: 9876543210

The macro checks the permitted search columns and returns the row when the value is found.


8. Partial Matching

A useful search utility should support partial text matching.

Suppose a cell contains:

ABC Technologies Private Limited

A search for:

Technologies

should still identify the record.

Similarly:

Search: 98765

could locate:

9876543210

This is useful when the complete value is unknown.

In VBA, this type of matching can be implemented using functions such as InStr.


9. Case-Insensitive Searching

For normal business-data searches, uppercase and lowercase letters generally should not affect the result.

For example, all of these may logically represent the same search:

MICROSOFT
Microsoft
microsoft
MicroSoft

Using case-insensitive comparison prevents records from being missed merely because capitalization differs between files.


10. Searching with Up to Three Criteria

A major improvement over a simple text finder is the ability to use multiple search conditions.

For example:

Criterion 1

Field: Company
Search: ABC

Criterion 2

Field: City
Search: Delhi

Criterion 3

Field: GSTIN
Search: 07

The macro can then combine these conditions using AND or OR logic.


11. Understanding AND / ALL Search

An ALL search means every specified condition must match the same record.

For example:

Company contains: ABC
AND
City contains: Delhi
AND
State contains: Delhi

A row is returned only when all applicable conditions are satisfied.

This is particularly useful for narrowing a large dataset.


12. Understanding OR / ANY Search

An ANY search means a record is returned when at least one condition matches.

For example:

Company contains: ABC
OR
Mobile contains: 98765
OR
Email contains: example.com

If any one of those conditions matches, the row can be included in the results.

This is useful when searching for a record using several possible identifiers.


13. Example of a One-Criterion Search

Suppose the user wants to find a GST number across all Excel files.

The search configuration could be:

Number of Criteria: 1

Field:
GSTIN

Search Text:
07ABCDE1234F1Z5

The program then searches the selected folder, its subfolders, supported Excel files, and relevant worksheets.

Every matching row is copied into the result sheet.


14. Example of a Two-Criterion Search

Suppose the requirement is to locate customers containing:

Company = Bison
City = Delhi

Choose:

Number of Criteria: 2

Criterion 1:

Field: Company
Value: Bison

Criterion 2:

Field: City
Value: Delhi

Search Mode:

ALL

The macro will return rows where both conditions match.


15. Example of a Three-Criterion Search

Suppose the user wants records matching:

Company = ABC
City = Delhi
Status = Active

Select:

3 Criteria

and use:

Company → ABC
City → Delhi
Status → Active

with:

ALL

This provides a more precise result than searching for one value alone.


16. Source File Information in Search Results

Finding the data is only half the job.

The user also needs to know where the result came from.

Therefore, the output should ideally include metadata such as:

Source File
Source Full Path
Source Sheet

Example:

Customer Mobile City Source File Source Sheet
ABC Ltd 9876543210 Delhi Customer2026.xlsx Customers

The full path may show:

D:\Customer Data\Delhi\2026\Customer2026.xlsx

This makes it much easier to locate and open the original workbook later.


17. Why Full File Path Is Important

Consider two files with the same name:

D:\Delhi\Customers.xlsx
D:\Mumbai\Customers.xlsx

Showing only:

Customers.xlsx

does not identify the source precisely.

Showing the complete path solves this problem:

D:\Delhi\Customers.xlsx

versus:

D:\Mumbai\Customers.xlsx

For a centralized search system, retaining the original file path is highly recommended.


18. Open Source Workbooks as Read-Only

A search program normally does not need to modify source files.

Therefore, workbooks should preferably be opened with:

ReadOnly = True

This reduces the chance of accidental modifications.

The process becomes:

Open source workbook as Read-Only
↓
Read/search data
↓
Copy matching information
↓
Close workbook without saving

The original workbook remains unchanged.


19. Ignore Excel Temporary Files

When an Excel workbook is open, Excel may create a temporary lock file beginning with:

~$

For example:

Customers.xlsx
~$Customers.xlsx

The temporary file should not be treated as a normal workbook.

A well-designed macro therefore ignores filenames beginning with:

~$

This helps prevent unnecessary errors.


20. Handling Files That Cannot Be Opened

Not every workbook will necessarily open successfully.

Possible reasons include:

  • Corrupted workbook
  • Password-protected workbook
  • Unsupported file structure
  • Permission restrictions
  • Network problems
  • File already locked
  • Damaged Excel file
  • File extension not matching the real file format

The search system should therefore handle errors gracefully.

Instead of terminating the entire operation because one workbook fails, it can skip that workbook and continue processing the remaining files.

At the end, the macro can report:

Files searched successfully: 247
Matching rows found: 38
Files skipped/failed: 3

This is much more useful than stopping at the first problematic file.


21. Performance Considerations

Searching hundreds of Excel files can be resource-intensive.

Opening workbooks one by one, reading individual cells, updating the screen, and recalculating formulas can significantly reduce performance.

Several VBA optimization techniques can help.

For example:

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

After the search finishes, the original settings should be restored.


22. Why Reading Data into Arrays Is Faster

One of the biggest VBA performance improvements is avoiding repeated cell-by-cell operations.

Instead of repeatedly executing operations such as:

Cells(Row, Column).Value

the macro can load a complete range into a VBA array.

Conceptually:

Excel Worksheet
      ↓
Load Range into Memory
      ↓
Search VBA Array
      ↓
Collect Matches
      ↓
Write Results Back to Excel

Searching data in memory is usually substantially faster than repeatedly accessing worksheet cells.


23. Writing Results in Batches

The same principle applies when writing results.

Instead of:

Write Row 1
Write Row 2
Write Row 3
Write Row 4
...

the program can prepare an output array and write multiple records to the worksheet in one operation.

This is particularly beneficial when thousands of matching rows are found.


24. Displaying Search Progress

A search involving many files may take time.

Without feedback, users may think Excel has frozen.

The VBA macro can use the Excel status bar to display information such as:

Searching file 48 of 327:
D:\Customer Data\Delhi\Customers.xlsx

This provides useful confirmation that the search is progressing.


25. Recommended Result Structure

A practical output format could be:

A:P    Original Data
Q      Source File
R      Source Full Path
S      Source Sheet

For example:

A B C ... Q R S
ID Customer Mobile ... Source File Full Path Sheet
1001 ABC Ltd 9876543210 ... Delhi.xlsx D:\Data\Delhi.xlsx Customers

The source metadata makes the consolidated search results auditable and traceable.


26. Installing a VBA Search Macro

A typical installation process is:

  1. Open the required Excel workbook.
  2. Save it as an Excel Macro-Enabled Workbook (.xlsm) if necessary.
  3. Press Alt + F11.
  4. The Visual Basic Editor opens.
  5. Select Insert → Module.
  6. Paste the VBA search code into the module.
  7. Press Ctrl + S.
  8. Close the Visual Basic Editor.
  9. Return to Excel.
  10. Press Alt + F8.
  11. Select the required search macro.
  12. Click Run.

If Excel displays a security warning, macros must be enabled only when the workbook and VBA code are from a trusted source.


27. Macro Security Considerations

VBA macros are powerful because they can interact with files, folders, workbooks, and Windows resources.

For this reason, macro-enabled files should be handled carefully.

Do not enable macros in unknown or suspicious workbooks downloaded from untrusted sources.

For internally developed utilities, organizations may consider:

  • Trusted Locations
  • Digitally signed VBA projects
  • Controlled deployment
  • Code review
  • Version control
  • Restricted access to master search workbooks

28. Important Limitation: Password-Protected Files

If a workbook requires a password to open, a normal automated search may not be able to process it.

Such files should generally be reported as skipped.

Passwords should not be hard-coded into VBA unless there is a properly assessed business and security requirement.


29. Searching Network Folders

The same concept can be used for folders located on:

  • Local drives
  • External drives
  • USB drives
  • Mapped network drives
  • File servers
  • NAS storage

For example:

D:\Customer Data\

or:

Z:\Accounts\

or a UNC path such as:

\\Server01\Accounts\

Performance will depend heavily on network speed, server performance, file count, workbook size, and concurrent usage.


30. Search Performance on Large Data Collections

Suppose a selected folder contains:

1,000 Excel files

and each workbook contains:

10,000 rows

The potential search space is approximately:

10,000,000 rows

A VBA solution can still be useful, but the design becomes important.

Performance can be affected by:

  • Number of files
  • Number of worksheets
  • Number of rows
  • Number of columns
  • Formula complexity
  • Workbook size
  • Network latency
  • Computer RAM
  • Storage speed
  • Number of search conditions

For extremely large datasets, a database, Power Query, SQL Server, SQLite, Access, or another indexing system may eventually be more suitable than repeatedly opening every workbook.


31. When VBA Is the Right Solution

A VBA folder-search utility is particularly useful when:

  • Existing data must remain in Excel files.
  • Users already work primarily in Microsoft Excel.
  • Installing a database server is undesirable.
  • The number of files is manageable.
  • Search requirements are relatively straightforward.
  • Results need to be available immediately in Excel.
  • The organization wants a portable internal utility.

32. When a Database May Be Better

VBA may become inefficient when there are millions of frequently searched records.

For example:

5,000 workbooks
50,000 rows per workbook
Frequent searches by many users

In that scenario, repeatedly opening thousands of files for every search is not efficient.

A better architecture may be:

Excel Files
     ↓
Import / Index
     ↓
Central Database
     ↓
Search Interface

Possible technologies include:

  • Microsoft Access
  • SQL Server
  • MySQL
  • PostgreSQL
  • SQLite
  • Power Query
  • Power BI

The correct solution depends on data volume and operational requirements.


33. Possible Future Improvements

A VBA search system can be expanded considerably.

Useful enhancements include:

  • Browse Folder button
  • Dedicated search form
  • Dropdown list of available columns
  • Three visible search fields
  • AND/OR radio buttons
  • Exact match option
  • Partial match option
  • Starts With option
  • Ends With option
  • Date range search
  • Numeric range search
  • File type selection
  • Worksheet selection
  • Search progress bar
  • Cancel Search button
  • Export Results button
  • Open Source File button
  • Open Source Folder button
  • Duplicate removal
  • Result sorting
  • Search history
  • Search log
  • File error log

A dedicated UserForm can make the system feel more like a small desktop application than a traditional macro.


34. Recommended User Interface

Instead of repeatedly displaying InputBox windows, a professional implementation could have a worksheet or VBA UserForm containing:

-----------------------------------------------------
              EXCEL DATA SEARCH
-----------------------------------------------------

Search Folder:
[D:\Customer Data\                    ] [Browse]

Criterion 1:
[Customer Name ▼] [ABC Technologies          ]

Criterion 2:
[City ▼]          [Delhi                     ]

Criterion 3:
[GSTIN ▼]         [07                        ]

Search Logic:
(o) ALL Conditions
( ) ANY Condition

File Types:
[x] XLSX
[x] XLSM
[x] XLS
[x] XLSB
[x] CSV

Include Subfolders:
[x] Yes

                 [ SEARCH ]
-----------------------------------------------------

This would be easier for regular office users and would reduce the need to understand VBA.


35. Conclusion

Searching manually through large numbers of Excel files is slow, repetitive, and prone to human error.

A VBA-based recursive Excel search utility can provide a practical middle ground between manually opening workbooks and implementing a full database system.

The most useful implementation should allow the user to select any desired folder, automatically inspect its subfolders, search multiple Excel file formats, inspect multiple worksheets, search by field or across multiple columns, combine up to three conditions, and return all matching records in one consolidated worksheet.

Adding the source filename, complete source path, and worksheet name to every result is particularly important because it allows users to trace a matching record back to its original location.

For organizations maintaining operational information in numerous Excel files, this type of tool can significantly reduce the time required to locate historical and current records.


Frequently Asked Questions (FAQ)

1. Can the macro search all Excel files in a selected folder?

Yes. The macro can scan supported Excel files contained in the folder selected by the user.

2. Can it search subfolders automatically?

Yes. A recursive VBA routine can search the selected folder and all accessible subfolders underneath it.

3. Which Excel file formats can be searched?

The macro can be designed to support .xlsx, .xlsm, .xlsb, .xls, and .csv.

4. Can it search CSV files too?

Yes. CSV files can be opened and searched along with Excel workbooks.

5. Do I have to select every subfolder manually?

No. You select the main folder once, and the recursive routine can process its subfolders automatically.

6. Can I search only one column?

Yes. You can specify a column letter, column number, or supported header name.

7. Can I search all columns?

Yes. An ANY field option can be used to search across the configured searchable columns.

8. Can I search using a header name?

Yes. For example, you may search fields such as Customer Name, Mobile, Email, or GSTIN, provided the macro is designed to resolve those headings.

9. Is the search case-sensitive?

It does not need to be. VBA can perform case-insensitive matching so MICROSOFT, Microsoft, and microsoft are treated equivalently for search purposes.

10. Can partial words be searched?

Yes. Partial matching can be implemented using VBA's InStr function.

11. Can I search using two conditions?

Yes. A multiple-criteria design can support two conditions.

12. Can I search using three conditions?

Yes. The macro can support up to three conditions or more if further extended.

13. What does ALL mean?

ALL means every specified search condition must match the record. It is equivalent to logical AND.

14. What does ANY mean when combining conditions?

ANY means at least one of the specified conditions must match. It is equivalent to logical OR.

15. What does ANY mean as a search field?

When used as the field selection, ANY can mean search across all configured searchable columns rather than one specific column.

16. Will the original Excel files be modified?

They should not be. A properly designed search macro opens source workbooks as read-only and closes them without saving.

17. Can the macro tell me which file contained the result?

Yes. The result can include the source filename.

18. Can it show the complete folder path?

Yes. Including the complete source path is recommended.

19. Can it tell me which worksheet contained the record?

Yes. The worksheet name can be included with every matching result.

20. Will it search every worksheet?

It can be designed to loop through every worksheet in each source workbook.

21. What happens if one Excel file is corrupted?

Good error handling allows the macro to skip the problematic file and continue searching other files.

22. Can it search password-protected files?

Normally not without supplying the required password. Such files may need to be skipped.

23. Can it search a network drive?

Yes, provided the Windows user has permission to access the network location.

24. Can it search a mapped drive such as Z:?

Yes, if the mapped drive is available to the current Windows session.

25. Can it search a USB drive?

Yes. Select the required folder on the USB drive.

26. Can it search thousands of files?

Technically it can, but search time increases as the number and size of files increase.

27. Why does Excel sometimes appear busy during the search?

Excel may be opening and processing many workbooks. A status indicator should be provided so users can see the current progress.

28. Why disable ScreenUpdating?

Disabling screen updates reduces unnecessary Excel interface redraws and can improve macro performance.

29. Why use manual calculation during searching?

Automatic calculation can cause source workbooks or the search workbook to recalculate repeatedly. Temporarily switching to manual calculation may improve performance.

30. Should calculation be restored afterward?

Yes. Application settings modified for performance should always be restored when the macro finishes or encounters an error.

31. Why use VBA arrays?

Reading ranges into memory can be significantly faster than examining worksheet cells individually.

32. Can the results be copied into one sheet?

Yes. Consolidating all matches into one result sheet is one of the main advantages of this approach.

33. Can duplicate results be removed?

Yes. Duplicate-removal functionality can be added.

34. Can I sort the final results?

Yes. VBA can automatically sort the result table after the search completes.

35. Can I filter the final results?

Yes. Excel AutoFilter or an Excel Table can be applied to the consolidated results.

36. Can the source file be opened directly from the result?

Yes. A future enhancement can create a hyperlink or button that opens the source workbook.

37. Can the source folder be opened from the result?

Yes. Windows Explorer can be launched from VBA using the stored source path.

38. Can I search an exact value instead of a partial match?

Yes. An Exact Match option can be added.

39. Can I search values beginning with specific text?

Yes. A Starts With comparison can be implemented.

40. Can date ranges be searched?

Yes, although dates should be handled as actual Excel dates rather than only as displayed text for reliable comparisons.

41. Can numeric ranges be searched?

Yes. Conditions such as Amount >= 50000 can be added with additional VBA logic.

42. Can I choose which file extensions to search?

Yes. A UserForm can provide checkboxes for XLSX, XLSM, XLSB, XLS, and CSV.

43. Can I exclude subfolders?

Yes. An Include Subfolders option can be added.

44. Does this require Microsoft Excel?

A VBA implementation requires a compatible desktop version of Microsoft Excel capable of running VBA macros.

45. Can Excel for the web run this VBA macro?

No. Traditional VBA macros are primarily intended for desktop Excel. Excel for the web does not run conventional VBA macros in the same way.

46. Why must the search workbook be XLSM?

The .xlsm format is required to preserve VBA macros inside an Excel workbook.

47. How do I open the VBA editor?

Press Alt + F11 in desktop Excel.

48. How do I run a macro manually?

Press Alt + F8, select the required macro, and click Run.

49. Should macros from unknown files be enabled?

No. Macros should only be enabled when the file and its VBA code come from a trusted source.

50. Is VBA the best solution for millions of frequently searched records?

Not always. For very large or frequently queried datasets, importing/indexing the information in a database such as SQL Server, MySQL, PostgreSQL, SQLite, or Access may provide substantially better performance and scalability.

#tags

#Excel #ExcelVBA #VBA #ExcelMacro #ExcelSearch #ExcelTips #ExcelAutomation #MicrosoftExcel #ExcelFiles #ExcelData #DataSearch #ExcelFolderSearch #FolderSearch #SubfolderSearch #RecursiveSearch #VBAMacro #VBAProgramming #ExcelProgramming #ExcelTutorial #ExcelTipsAndTricks #ExcelProductivity #ExcelTools #DataManagement #DataExtraction #DataProcessing #Spreadsheet #SpreadsheetAutomation #XLSX #XLSM #XLS #XLSB #CSV #CSVFiles #MultipleCriteria #AdvancedSearch #ExcelFilter #ExcelLookup #DataFinder #FileSearch #WorkbookSearch #WorksheetSearch #BusinessAutomation #OfficeAutomation #MicrosoftOffice #DataConsolidation #ExcelDatabase #VBADeveloper #ExcelSolutions #TechnicalGuide #KnowledgeBase

YOUR FEEDBACK

Was this guide useful?

Your answer helps us keep BISONKB accurate and practical.

BISON AI

Ask about “How to Search Data Across Multiple Excel Files, Folders and Subfolders Using a VBA Macro – Search XLSX, XLSM, XLS, XLSB and CSV Files with Multiple Criteria”

This interface is ready to connect to your preferred AI provider. No article or user data is sent until that service is configured.

THE BISON BRIEF

Practical IT knowledge, once a week.

New troubleshooting guides, scripts and infrastructure notes. No noise.

By subscribing, you agree to our privacy policy.