vba
4 replies
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?
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.
If row 1 is a header, check lastRow < 2 before processing records. This approach relies on the ID column being filled for every record.
Good point about the header-only case. Do you prefer working with Table rows when building a new workbook?