VBA & Macro Problems

Use Excel VBA to Copy Data Through the Last Populated Row

Maira MehtabMaira Mehtab Nov 25, 2026 869 views

Question details

The user wants an Excel VBA macro to copy a dataset between workbooks without hard-coding its final row or column.

Use Excel VBA to Copy Data Through the Last Populated Row
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A macro copies data from a source worksheet to a destination worksheet, starting below the headers.
Observed behavior
The existing macro uses a fixed range such as A2:G65000 rather than adapting to the populated dataset.
Before you start

Open both workbooks in Excel and confirm their names, worksheet names, and the location of the source headers. The examples assume headers start at A1 and data should be pasted at A2 in the destination sheet.

Solution 1Recommended

Copy a Contiguous Dataset with CurrentRegion

Use CurrentRegion when the dataset around A1 has no completely blank separating rows or columns.

CurrentRegion expands to the surrounding block of cells. The macro skips the first row and reduces the copied range by one row so it does not include an extra row below the dataset.

1
Open the VBA editor

In Excel, select Developer > Visual Basic, then choose Insert > Module.

2
Add the macro

Paste this code into the module, adjusting the workbook and sheet names if needed: Sub CopyCurrentRegionData() Dim sourceSheet As Worksheet Dim destinationSheet As Worksheet Dim dataRegion As Range Set sourceSheet = Workbooks("ClientData.xls").Sheets("ClientInfo") Set destinationSheet = Workbooks("ClientBio_v2.xlsm").Sheets("ClientInfo") Set dataRegion = sourceSheet.Range("A1").CurrentRegion If dataRegion.Rows.Count < 2 Then Exit Sub dataRegion.Offset(1, 0).Resize(dataRegion.Rows.Count - 1, dataRegion.Columns.Count).Copy _ Destination:=destinationSheet.Range("A2") End Sub

3
Run the macro

Return to Excel, select Developer > Macros, choose CopyCurrentRegionData, and select Run. Check the destination sheet starting at A2.

Copy a Contiguous Dataset with CurrentRegion
Check the dataset boundaries: A completely blank row or column can separate part of the dataset from CurrentRegion. The macro also does not clear older destination data beyond the newly copied range.
Free Microsoft Office alternative

Work with Excel Spreadsheets in WPS Office

WPS Office is a free, lightweight alternative for opening and editing spreadsheets in a familiar interface. Its Excel format support makes it easier to move existing files to WPS, although it does not repair Excel VBA macros and macro compatibility can vary.

Open and edit Microsoft Excel XLS and XLSX spreadsheets.Review and copy spreadsheet data in a familiar interface.Use a free, lightweight office suite for everyday spreadsheet work.Move existing spreadsheets to WPS without starting from scratch.
microsoft office alternative - wps office

Frequently Asked Questions

Why does CurrentRegion stop before the end of my data?

CurrentRegion stops at completely blank separating rows or columns. Remove the separators if appropriate, or use the Find-based macro to determine the last used row and column.

How can I copy the headers as well as the data?

In the CurrentRegion macro, replace the Offset and Resize copy statement with dataRegion.Copy Destination:=destinationSheet.Range("A1"). This copies the entire region, including its first row.

Do both workbooks need to be open?

Yes. These examples refer to open workbooks through the Workbooks collection. Open both files in Excel before running the macro, or add code to open them first.

Will the macro remove old rows from the destination sheet?

No. The copy overwrites the destination cells it reaches but leaves older data outside that range. Clear the relevant destination area first if those leftover cells are not needed.

Can WPS Office run the same VBA macro?

Macro support and compatibility can vary by WPS Office version and configuration. Test the macro in your installation before relying on it, especially when working with macro-enabled files.