How to Copy Excel Columns by Header Between Workbooks Using VBA
Question details
The user needs to dynamically copy data from specific columns in a source workbook to a destination workbook by matching their header names rather than their fixed column letters.

- Product
- Microsoft Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Merging or extracting data from reports where column layouts or orders frequently change, making hardcoded column references unreliable.
- Observed behavior
- The goal is to accurately identify source columns by their header text, copy the respective data ranges, and paste them under the corresponding headers in the destination workbook.
Ensure both the source and destination workbooks are open in the same application instance, and save a backup copy of your destination file before running any new macro to prevent accidental data overwriting.
Use the Range.Find Method in VBA to Match Headers
This method uses a VBA script to loop through the destination headers, locate the matching header in the source file, and copy the corresponding column data automatically.
Using the Range.Find method with the LookAt:=xlWhole parameter ensures an exact match of your column headers. This prevents the script from accidentally copying a column named 'Subtotal' when it was looking for 'Total'. Fully qualifying your workbook and worksheet references prevents data from being copied from or to the wrong active window.
Open the target workbook where you want to paste the data. Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor.
In the VBA Editor, click on 'Insert' in the top menu bar, and then select 'Module' to create a blank script window.
Copy and paste the following VBA code into the module window: Sub CopyColumnsByHeader() Dim SourceHeadings As Range, DestHeadings As Range Dim SourceHeader As Range, DestHeader As Range Dim Source As Range, Dest As Range Set SourceHeadings = Workbooks("SourceData.xlsm").Sheets("Table").Range("A1:G1") Set DestHeadings = ThisWorkbook.Sheets(1).Range("A1:B1") For Each DestHeader In DestHeadings Set SourceHeader = SourceHeadings.Find(DestHeader.Value, LookIn:=xlValues, LookAt:=xlWhole) If Not SourceHeader Is Nothing Then With SourceHeader Set Source = .Parent.Range(.Offset(1), .Offset(.Parent.Rows.Count - .Row).End(xlUp)) End With With DestHeader Set Dest = .Parent.Cells(.Parent.Rows.Count, .Column).End(xlUp).Offset(1) End With Source.Copy Destination:=Dest End If Next DestHeader Application.CutCopyMode = False End Sub Important: Update the file name ('SourceData.xlsm') and sheet names to match your actual files.
Press F5 or click the 'Run' button (the green play icon) on the toolbar to execute the macro. The script will locate each header and copy the respective column data below your destination headers.

Execute Macros and VBA Seamlessly with WPS Office
WPS Office provides robust support for macros and VBA, allowing you to run your Excel automation scripts directly within WPS Spreadsheet without rewriting your code.
- 1. Open Your Files in WPS: Launch WPS Spreadsheet and open both your source workbook and the destination .xlsm file.
- 2. Access the Macro Editor: Navigate to the 'Tools' tab on the top ribbon and click on 'Macro', or simply press Alt + F11 to open the WPS Macro Editor.
- 3. Insert Your Script: Go to Insert > Module and paste your column-matching VBA script into the code window.
- 4. Run the Automation: Click 'Run' to execute the script and watch WPS Spreadsheet automatically match headers and copy your columns accurately.

Frequently Asked Questions
Why does the VBA code fail to find my headers?
This usually happens if there are trailing spaces in your header cells, or if the header names don't match exactly. The LookAt:=xlWhole parameter requires a perfect match. Check for hidden spaces or use the TRIM function on your header cells before running the macro.
Can I modify this code to look for headers in a different row?
Yes. In the script, adjust the target ranges (e.g., Range("A1:G1")) to the specific row where your headers reside. For example, change it to Range("A5:G5") if your table headers start on row 5.
How do I avoid getting a 'Subscript Out of Range' error?
Make sure that both the source and destination workbooks are open in the exact same application instance. Also, verify that the workbook file extensions (like .xlsx or .xlsm) and sheet names written in your code perfectly match the actual open files.
Will this macro overwrite existing data in the destination column?
No. The provided script uses the .End(xlUp).Offset(1) method to find the last used row in the destination column. It will paste the newly copied data directly below the existing data, appending it rather than overwriting.




