VBA & Macro Problems

Fix VBA Overwriting Rows When Copying Multiple Excel Files

Bushra ParveenBushra Parveen Nov 2, 2026 870 views

Question details

The user is attempting to copy data from multiple Excel workbooks into a single master sheet using VBA, but the macro is pasting all results into the same row or starting at an incorrect cell reference (e.g., row 20).

How to Fix VBA Overwriting Rows When Copying Multiple Excel Files to a Master Sheet
Product
Excel
Device & OS
not provided
Scenario
Running a VBA macro to combine structured data from several individual Excel files into one consolidated master worksheet.
Observed behavior
The VBA code overwrites existing data because it fails to increment the destination row after processing each workbook, or pastes the data starting at the wrong row due to incomplete code.
Before you start

Before modifying your VBA code, ensure all source workbooks are saved and closed, and verify that the target master sheet has clear, consistent column headers starting at row 1.

Solution 1Recommended

Update the VBA Code to Increment the Destination Row Dynamically

Use this method to fix macros that overwrite data by recalculating the last empty row in the master sheet before each paste operation.

When a loop processes multiple files, a static destination row variable will cause the macro to paste new data over the old data. By implementing a dynamic calculation like 'End(xlUp)', the script will always find the next available empty row.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications (VBA) editor, and locate the module containing your merge code.

2
Locate the Paste Command

Find the section of your loop where the copied data from the source workbook is pasted into the master sheet.

3
Add Dynamic Row Calculation

Right before the paste command, insert a line of code to find the next empty row. For example: `Dim LastRow As Long` followed by `LastRow = ThisWorkbook.Sheets("Master").Cells(Rows.Count, 1).End(xlUp).Row + 1`.

4
Update the Destination Range

Modify your paste destination to use the new variable. Change ranges like `Range("A2")` to `Range("A" & LastRow)` so the code targets the newly calculated blank row.

Update the VBA Code to Increment the Destination Row Dynamically
Fixing Incorrect Starting Rows: If your macro starts pasting at row 20 instead of A1, ensure that rows 1 through 19 do not contain hidden formatting, spaces, or merged cells, as the End(xlUp) method detects these as occupied rows.
Combine Workbooks Without Code

Easily Merge Multiple Excel Files in WPS Spreadsheet

WPS Spreadsheet offers powerful built-in data tools and full VBA support. You can seamlessly run your existing macros or use intuitive menu options to merge multiple workbooks into a master sheet without troubleshooting complex code.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and create a new blank workbook to serve as your master consolidation sheet.
  2. 2. Access Data Tools: Navigate to the 'Data' tab on the top ribbon and locate the data consolidation or workbook merging features.
  3. 3. Select Source Files: Add all the individual Excel files (.xlsx or .csv) that you want to combine into the master sheet.
  4. 4. Merge and Save: Confirm the ranges you wish to merge and click 'OK'. WPS Spreadsheet will automatically stack the data perfectly into your master sheet.
Built-in Smart Toolbox to merge multiple files and worksheets instantlyFull compatibility with Microsoft Excel VBA macros and .xlsx formatsNo advanced programming required for standard data consolidationLightweight, fast, and highly intuitive user interface
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA macro overwrite the previous data instead of listing it below?

This occurs because the variable determining the destination row is either static (e.g., always targeting row 2) or is calculated outside the loop. To fix this, you must recalculate the last empty row dynamically inside the loop before every paste action.

Why does the combined data start at row 20 instead of cell A1?

If data unexpectedly starts further down the sheet, rows 1 through 19 likely contain hidden characters, invisible formatting, or stray spaces. The VBA 'End(xlUp)' method sees these as active cells. Clearing all formats from the seemingly empty rows above usually resolves this.

Is Power Query better than VBA for merging multiple Excel files?

For structured tabular data, Power Query is generally superior. It is easier to set up, requires no coding, automatically manages column alignment, and easily handles thousands of rows without the freezing issues commonly caused by heavy VBA loops.

Can I run my existing Excel VBA scripts in WPS Office?

Yes, WPS Office provides excellent support for VBA macros. You can open your Excel files containing macros in WPS Spreadsheet, and scripts designed to merge workbooks or format data will run identically.