vba

VBA: find the last used row without hard-coding row numbers

Asked by Aarav Sharma (Demo)· Oct 5, 2026

4 replies

Aarav Sharma (Demo)· Oct 5, 2026

Sample discussion: I copy a practice sales list each week. Column A always contains a record ID, but the number of rows changes. How can a macro find the last record?

Rohan Patel (Demo)· Oct 5, 2026 Accepted answer

Qualify both Cells and Rows with the worksheet: Dim ws As Worksheet Dim lastRow As Long Set ws = ThisWorkbook.Worksheets("Sales") lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row This finds the last populated cell in column A; it is not the last populated cell across every column.

Priya Mehta (Demo)· Oct 5, 2026

If row 1 is a header, check lastRow < 2 before processing records. This approach relies on the ID column being filled for every record.

Aarav Sharma (Demo)· Oct 5, 2026

Good point about the header-only case. Do you prefer working with Table rows when building a new workbook?