The quickest way to get the last used row in Excel VBA is Cells(Rows.Count, 1).End(xlUp).Row. That line jumps from the bottom of column A upward to the last non-empty cell, and it is the method most macro writers reach for first. It works reliably on any normal worksheet, needs no add-ins, and runs in milliseconds. But it has a catch: it only looks at one column, so it can miss data that extends further down in a different column. Below you will find that method plus the alternatives, when each one is correct, and the traps that make macros return the wrong number.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Developer tab visible (if you do not see it, right-click the ribbon and choose Customize the Ribbon, then tick Developer).
  • A workbook containing the data you want to measure, ideally saved as .xlsm so macros persist.
  • Basic familiarity with opening the Visual Basic Editor: press Alt+F11 on Windows, or Option+F11 on a Mac.
  • An empty standard module: in the editor choose Insert > Module. Note that menu labels can differ slightly between Excel versions, and Mac menus sit in a different place than Windows ones.

Step-by-Step

  1. Open the editor and add a module. Press Alt+F11 (Windows) or Option+F11 (Mac), then use Insert > Module. All code below goes in a normal module, not in a sheet's code-behind, unless you deliberately want it to fire on an event.
  2. Write the standard one-column method. Type:
    Dim lastRow As Long
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    MsgBox lastRow
    Rows.Count returns the last row of the sheet (1,048,576 in modern .xlsx files). End(xlUp) mimics pressing Ctrl+Up, stopping at the last filled cell in column A. Assign the result to a Long, never an Integer, because row numbers exceed the Integer limit.
  3. Run it and confirm the number. Place the cursor inside the Sub and press F5. Compare the message box with what you see in column A. If it says 1 on an empty sheet, that is correct behaviour, not a bug.
  4. Use it to drive a range. A typical use is looping from row 2 to the last row:
    For i = 2 To lastRow
    Debug.Print Cells(i, 1).Value
    Next i
    This is the pattern behind almost every data-cleaning or report macro.
  5. Extend to the true last row across all columns. If data runs down column A but also further in column D, measure the whole used area:
    lastRow = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1
    UsedRange covers every cell Excel considers used, so this finds the lowest occupied row anywhere on the sheet.
  6. Or use Find, the most robust option. To find the last row that contains anything at all:
    lastRow = Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Searching backwards from the bottom of the sheet finds the last cell with data regardless of column. If the sheet is truly empty, Find returns Nothing and the line errors, so guard it with If Not Cells.Find(...) Is Nothing Then.
  7. Pick the right column deliberately. For a table where column A holds an ID or date that is always populated, the one-column method is faster and simpler. For a messy sheet with gaps, use UsedRange or Find. Write down which assumption your data satisfies before you choose.
  8. Qualify the worksheet, then test on bad data. Replace bare Cells and Rows with explicit references such as ws.Cells(ws.Rows.Count, 1).End(xlUp).Row, where ws is set with Set ws = ThisWorkbook.Worksheets("Data"). Then add a blank row, a hidden row and a formula returning an empty string, and run again to see how each method responds.

Common Mistakes

  • Declaring an Integer. Dim lastRow As Integer overflows above row 32,767 and throws a runtime error. Always use Long.
  • Starting End(xlUp) from row 1. Range("A1").End(xlUp) only inspects the first row, because Ctrl+Up from A1 goes nowhere. Start from the last row of the column.
  • Trusting End(xlDown). Range("A1").End(xlDown).Row stops at the first blank cell, so a single gap makes it report a wrong answer. It is also the slowest to spot because it looks like the right answer on clean data.
  • Assuming UsedRange is always tight. Deleted rows and formatting can leave UsedRange larger than the visible data, inflating your count. Press Ctrl+End to see what Excel believes the last cell is.
  • Forgetting formulas that return "". A cell containing ="" looks blank but counts as non-empty, so both End(xlUp) and UsedRange will treat it as data. Filter or test for zero-length strings if this matters.
  • Leaving out the sheet qualifier. Unqualified Cells refers to the active sheet, not the one you meant, so the macro returns the wrong row after a user switches tabs.
  • Reading Code incorrectly. Rows.Count is already correct; Rows.Count - 1 skips the final row and can miss real data.

Tips

  • Wrap the whole thing in a tiny helper function so you write it once and reuse it: Function LastRow(ws As Worksheet, col As Long) As Long with the End(xlUp) line inside.
  • For a last column, swap the logic: Cells(1, Columns.Count).End(xlToLeft).Column.
  • Turn off ScreenUpdating during long loops that use the last row, then restore it, so the screen does not flicker.
  • When writing data below existing rows, calculate the last row once before the loop, not on every iteration, or the target row keeps moving.
  • Test with at least three scenarios: empty sheet, single row, and a sheet with a gap. That trio exposes nearly every wrong method.

Troubleshooting

If the macro returns the wrong row, first check whether column A is actually the populated column; switch the column number in Cells(Rows.Count, 1) and rerun. If it returns 1 unexpectedly, the sheet may be empty or the data may sit in a different worksheet than the active one, so add an explicit sheet reference. If you get a run-time error 1004 on the Find method, the sheet has no data and Find returned Nothing; add the Nothing guard described above.

If the number is too large, look for leftover formatting or stray values far down the sheet: select all rows below the real data and delete them, then save. For an "overflow" or "type mismatch" error, confirm the variable is declared As Long and that you assigned the result of .Row, not the range object itself. If nothing runs at all, check that macros are enabled for the file and that the code is in a module rather than a worksheet's event area.

Frequently Asked Questions

Which method should I use by default?

Cells(Rows.Count, 1).End(xlUp).Row for a clean single column of data; UsedRange or Find when the data is messy or spread across columns.

Why does End(xlUp) return the last row of the whole sheet?

Because Rows.Count is being used in place of a real column index, or the start cell is below all data on a sheet with something in the very last row. Check the column argument and delete stray values at the bottom.

Can I get the last row of a specific table instead of the sheet?

Yes. Reference the table's range, for example ws.ListObjects("Table1").Range.Rows.Count, and add the table's first row offset to convert the count into a sheet row number.

Does this work in Excel for Mac?

Yes, the same object model applies; only the keyboard shortcut to open the editor differs (Option+F11).

How do I get the last row when column A has blanks?

Use the Find method with SearchDirection:=xlPrevious, or measure UsedRange. End(xlUp) will stop at the first non-empty cell above the blank, so it can under-report.