VBA & Macro Problems

How to Combine Excel Workbooks While Keeping One Header Row

Huda QurayshiHuda Qurayshi Oct 26, 2026 868 views

Question details

The user wants to combine data from multiple Excel workbooks into a single master sheet without duplicating the header row from each source file.

How to Combine Excel Workbooks While Keeping One Header Row
Product
Excel
Device & OS
not provided
Scenario
Merging data from several separate workbooks into a consolidated report using a VBA macro.
Observed behavior
The current VBA procedure successfully combines the files but incorrectly copies the header row from every single source file into the final dataset.
Before you start

Ensure all of your source workbooks have an identical column structure and that your master workbook is saved as a Macro-Enabled Workbook (.xlsm).

Solution 1Recommended

Use a Boolean Variable in Your VBA Macro

Modify your existing VBA loop to check whether the macro is processing the very first file. If it is, copy the header row; otherwise, skip the header and only copy the data rows.

By introducing a simple Boolean (True/False) variable, you can instruct your VBA script to treat the first workbook differently from the rest. This ensures the header is established once and ignored in subsequent loops.

1
Declare the Boolean Variable

Open your VBA editor (Alt + F11). At the beginning of your subroutine, declare the variable by typing: Dim OnlyOnce As Boolean

2
Initialize the Variable

Before the script enters the loop that opens each workbook, set the variable to True by typing: OnlyOnce = True

3
Add Conditional Copy Logic

Inside your loop, replace your standard copy command with an IF statement. Type: If OnlyOnce = True Then [Copy Range starting at A1] Else [Copy Range starting at A2]

4
Toggle the Variable

Immediately after the copy command inside the loop, set the variable to False by typing: OnlyOnce = False. This ensures all subsequent workbooks will trigger the 'Else' condition and skip row 1.

Use a Boolean Variable in Your VBA Macro
Dynamic Range Copying: Make sure your copy ranges dynamically calculate the last used row so you don't accidentally copy blank rows into your master sheet.
Efficient Data Management

Merge Workbooks Effortlessly with WPS Spreadsheet

WPS Spreadsheet fully supports VBA macros and provides intuitive data management tools, making it easy to automate repetitive tasks like merging multiple workbooks without duplicating headers.

  1. 1. Open the VBA Editor: Launch WPS Spreadsheet and press Alt+F11 to open the built-in VBA Editor.
  2. 2. Insert Your Code: Paste your modified workbook-combining VBA script with the Boolean logic into a new Module.
  3. 3. Run the Macro: Execute the macro to instantly compile your source files into a single master sheet with perfectly preserved headers.
Fully compatible with Microsoft Excel formats, including .xlsx and .xlsmComprehensive VBA/Macro support for automated data merging and scriptingLightweight application that handles large datasets smoothlyFree to use with a familiar, user-friendly interface that minimizes learning curves
microsoft office alternative - wps office

Frequently Asked Questions

Why does my combined Excel file have multiple header rows?

When looping through multiple files, a basic copy-paste script selects the entire data range including row 1 from every file. You must explicitly tell the script to start from row 2 (e.g., cell A2) for all files after the very first one.

Can I use VBA to combine files from a specific folder?

Yes, you can use the Dir function in VBA to loop through all Excel files within a specified directory path, opening, extracting data, and closing each one sequentially.

How can I remove duplicate header rows if I already combined the files?

If you already ran a script that copied headers, you can easily clean the data by selecting your dataset, applying a Filter (Ctrl+Shift+L), filtering the first column for your header name, and deleting those specific rows.

Does WPS Office support Excel VBA macros?

Yes, WPS Office fully supports standard VBA macros. You can open, edit, and run your Excel .xlsm scripts directly in WPS Spreadsheet without rewriting your code.