Excel VBA: Autofill Two Columns to the Last Used Row
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.

- 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.
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.
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.
In Excel, open the Developer tab and select Visual Basic. In the VBA editor, choose Insert > Module.
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
Replace "Sheet1" with the worksheet name and change "A" if another column defines the last row. Run AutofillTwoColumns from Developer > Macros.

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.

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.




