How to Extract Data for Multiple Column Headers Using Excel VBA
Question details
The user needs to write a VBA procedure to search for multiple column headers by name and extract the data beneath them.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Writing a macro to dynamically find specific columns based on their header text and extract or copy the rows of data below them.
- Observed behavior
- Assigning multiple header names to a single String variable causes VBA to fail. The procedure requires storing the multiple headers in an array to properly loop through and extract the desired columns.
Ensure you have the Developer tab enabled in your spreadsheet ribbon to access the VBA editor, and save a backup copy of your workbook before running new macros.
Use a Variant Array to Store Headers and Extract Data
Declare your target headers as a Variant array to prevent type mismatch errors, then iterate through the worksheet to find matches and copy the data below them.
When searching for multiple headers, assigning an array of strings directly to a standard String variable will result in an error. Instead, declare the variable as a Variant and use the Array function. This allows you to construct a 'For Each' loop to iterate through every header name systematically.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications editor. Go to Insert > Module from the top menu to create a new blank script area.
In your new procedure, type 'Dim headers As Variant' to declare the variable. Assign your specific header names to it using the array function, for example: headers = Array("Category", "Names").
Write a 'For Each header In headers' loop. Inside the loop, use the Range.Find method combined with 'CStr(header)' to search the top row (e.g., Rows(1)) for the exact text of the current header.
Add an 'If Not foundCell Is Nothing' statement to check if the header was found. If it exists, define the range directly below it using 'Range(foundCell.Offset(1, 0), Cells(Rows.Count, foundCell.Column).End(xlUp))' and copy the values to your destination sheet.
Close the VBA editor or press F5 while inside the module to execute the code. The macro will systematically search for each header in your array and pull the respective column data.

Automate Data Extraction with WPS Spreadsheet
WPS Office provides excellent support for VBA and macros, allowing you to easily automate tasks like extracting data from multiple columns. Write, edit, and execute your VBA scripts directly within WPS Spreadsheet's familiar interface.
- 1. Enable Macros: Open your workbook in WPS Spreadsheet and navigate to the Developer tab on the top ribbon.
- 2. Launch the Editor: Click on 'Visual Basic' or 'WPS Macro' to launch the integrated code editor.
- 3. Run Your Code: Paste your Variant array extraction code into a new Module and click the Run button to instantly gather your column data.

Frequently Asked Questions
Why do I get a "Type Mismatch" error when storing multiple headers in a String variable?
A standard String variable is designed to hold a single text sequence. To store a list of multiple header names (like "Category" and "Names") from the Array() function, you must declare the variable as a Variant, which can contain an array structure.
Can I use this VBA array method to search across multiple worksheets at once?
Yes. You can nest your header extraction loop inside an outer loop that iterates through all sheets (e.g., For Each ws In ActiveWorkbook.Worksheets). Just ensure you qualify your Range and Cells references with the 'ws' object so the macro searches the correct sheet.
What happens if my Excel macro searches for a header that isn't there?
If you use the Range.Find method and the header is missing, it returns 'Nothing'. If you try to extract data without checking this first, the macro will throw an Object Required error. Always wrap your extraction step in an 'If Not foundCell Is Nothing Then' block.




