VBA & Macro Problems

Excel VBA: Autofill Two Columns to the Last Used Row

Ayan MasoodAyan Masood Dec 2, 2026 868 views

Question details

The user needs a VBA macro that fills two columns from starting cells through the last populated row without selecting ranges or using a fixed ending row.

Autofill Two Columns to the Last Used Row with Excel VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
Copy formulas or values from starting cells in columns AA and AB down to the end of a worksheet's data.
Observed behavior
The macro should determine the ending row dynamically and fill both columns directly.
Before you start

Identify a column that is populated through the intended last data row; the example uses column A. Place the formulas or values to copy in AA2 and AB2, and check that replacing any existing content below them is acceptable.

Solution 1Recommended

Autofill AA and AB using a dynamic last row

Use a worksheet-qualified range and the last populated row in column A to fill both columns without selecting cells.

This example assumes the source cells are AA2 and AB2 and that column A determines the end of the data. Change the worksheet name, reference column, and starting row to match your workbook.

1
Open the VBA editor

In Excel, open the Developer tab and select Visual Basic. In the VBA editor, choose Insert > Module.

2
Add the macro

Paste this code into the module: Sub AutofillTwoColumns() Dim ws As Worksheet Dim lastRow As Long Set ws = ThisWorkbook.Worksheets("Sheet1") lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row If lastRow > 2 Then ws.Range("AA2:AB2").AutoFill _ Destination:=ws.Range("AA2:AB" & lastRow), _ Type:=xlFillCopy End If End Sub

3
Adjust and run the macro

Replace "Sheet1" with the worksheet name and change "A" if another column defines the last row. Run AutofillTwoColumns from Developer > Macros.

Autofill AA and AB using a dynamic last row
How the fill works: The macro does nothing if the last row in the reference column is row 2 or earlier. It overwrites the destination cells below AA2 and AB2; relative formula references adjust as they are copied.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday workbook tasks between applications. It does not fix an Excel VBA issue; check macro support before migrating a VBA-dependent workbook.

Fill formulas and values down spreadsheet columns using familiar controlsOpen and edit common Microsoft Excel formats, including XLSX and XLSUse a familiar interface for everyday spreadsheet workMove routine workbook tasks to a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

How does the macro find the last row?

It uses ws.Cells(ws.Rows.Count, "A").End(xlUp).Row to find the last nonempty cell in column A. Replace "A" with a column that reaches the intended end of your data.

Will the macro copy formulas as well as values?

Yes. AutoFill with xlFillCopy copies the contents of AA2 and AB2 down the destination. Relative references in formulas adjust for each row.

What if my starting cells are on a different row?

Change both instances of the starting row in the macro: update AA2:AB2, AA2:AB in the destination, and the If lastRow > 2 condition to use your starting row.

How do I keep the macro in my workbook?

Use File > Save As and select Excel Macro-Enabled Workbook (.xlsm). Only enable macros in workbooks you trust.