To get the last row in Excel VBA, the most reliable and standard method is using Cells(Rows.Count, "A").End(xlUp).Row. This command tells Excel to go to the very last cell of a specific column (like column A), then jump upward until it hits the first cell that contains data, returning that row number. This approach is superior to other methods because it ignores empty cells within your data range and finds the true bottom of your list.

What You'll Need

  • Microsoft Excel installed on a Windows or Mac computer.
  • The Developer tab enabled in the Excel Ribbon.
  • A basic understanding of how to open the Visual Basic for Applications (VBA) editor.
  • A worksheet containing a dataset with at least one column that is consistently filled (no gaps in the primary ID or date column).

Step-by-Step

  1. Open the VBA Editor: Press Alt + F11 on your keyboard. This shortcut opens the Visual Basic Editor window where you will write your automation scripts.
  2. Insert a New Module: In the Project Explorer on the left side, right-click on your workbook name, hover over Insert, and select Module. This creates a clean slate for your code to live in, separate from sheet-specific events.
  3. Declare Your Variables: Start your subroutine by declaring a variable to store the row number. Use a Long data type rather than Integer because modern Excel sheets can have over a million rows, which exceeds the capacity of an integer variable.
    Dim lastRow As Long
  4. Specify the Worksheet: It is a best practice to explicitly name the sheet you are working with to avoid the code running on the wrong tab.
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")
  5. Apply the End(xlUp) Logic: Use the following line of code to find the last row. This mimics the manual action of clicking the bottom cell of a column and pressing Ctrl + Up Arrow.
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Replace "A" with the letter of the column that is most likely to be fully populated for every record.
  6. Test the Result: To verify the code is working, add a message box that displays the number.
    MsgBox "The last row is " & lastRow
  7. Run the Macro: Press F5 or click the green Play button in the toolbar to execute the code.

Common Mistakes

One of the most frequent errors is using End(xlDown) instead of End(xlUp). While xlDown seems intuitive, it starts at the top and stops at the first empty cell it encounters. If your data has a single blank row in the middle of the set, xlDown will stop there, giving you an incorrect row count and causing your macro to miss half of your data. Always start from the bottom of the sheet and move up.

Another common mistake is failing to specify the worksheet. If you simply write Cells(Rows.Count, 1).End(xlUp).Row, VBA assumes you mean the ActiveSheet. If a user happens to have a different tab selected when the macro runs, the code will return the last row of that wrong tab, which can lead to data being deleted or overwritten in the wrong place.

Using the Integer variable type is a legacy mistake. In older versions of Excel, sheets were much smaller. In modern versions, an Integer can only hold a value up to 32,767. Since Excel supports 1,048,576 rows, any dataset larger than 32k will trigger an "Overflow" error. Always use Long for row counts.

Tips

If you are working with a Table (ListObject), you do not need to find the last row manually. You can reference the table's range directly using ws.ListObjects("TableName").Range.Rows.Count. This is more efficient and cleaner for structured data.

When using the last row variable to loop through data, remember that if you have a header row, your loop should start at lastRow + 1 or 2. For example, if you are adding a new entry to the bottom of a list, you will typically use lastRow + 1 as the destination for your new data to avoid overwriting the existing last entry.

To make your code more flexible, you can use a variable for the column index instead of a hardcoded "A". For instance, ws.Cells(ws.Rows.Count, 1) refers to column A, while ws.Cells(ws.Rows.Count, 2) refers to column B. This allows you to easily change which column the macro uses to determine the end of the dataset.

Troubleshooting

If the code returns 1 when you know there is data, check if your column is actually empty. The End(xlUp) method looks for any value; if the column you specified is blank, it will travel all the way up to row 1. Ensure you are referencing a column that is guaranteed to have data for every row.

If the code returns a number much larger than your actual data, you likely have "ghost cells." These are cells that appear empty but contain invisible formatting, spaces, or remnants of deleted data. To fix this, select the empty rows below your data, right-click, select Delete, and save the workbook. This clears the Excel used range and resets the row count.

If you receive a "Subscript out of range" error, it means the sheet name you provided in Sheets("Sheet1") does not exist. Double-check the spelling of your tab name, ensuring there are no trailing spaces in the tab name itself.

Frequently Asked Questions

What if my data has blank cells in the middle?

The End(xlUp) method is specifically designed for this. Because it starts at the very bottom of the spreadsheet (row 1,048,576) and moves upward, it will ignore any blanks in the middle of your data and only stop when it hits the absolute last piece of content in that column.

Can I find the last row across the entire sheet regardless of column?

Yes, but it requires a different method. You can use ActiveSheet.UsedRange.Rows.Count, though this can be unreliable if you have formatting in empty cells. A more robust way is to use the Find method: ws.Cells.Find("*").Row. This searches the entire sheet for any character and returns the row number of the last match found.

Does this work on Excel for Mac?

Yes, the Cells(Rows.Count, "A").End(xlUp).Row syntax is universal across both Windows and Mac versions of Excel VBA. The only difference may be the keyboard shortcuts used to open the editor.

How do I use the last row to create a dynamic range?

Once you have the lastRow variable, you can define a range like this: Set myRange = ws.Range("A2:B" & lastRow). This allows your macro to automatically adjust its scope as your dataset grows or shrinks over time.

Is there a way to find the last column using the same logic?

Absolutely. You simply swap the row and column references: lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column. This starts at the far right of the first row and moves left until it hits data, returning the column number.