Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Finding the Last-Used Cell in a Macro.
by Allen Wyatt
(last updated April 2, 2020)
If you are working in a worksheet, you know that you can press Ctrl+End to jump to the last-cell in the worksheet. The shortcut chooses the cell that represents the intersection of the last column containing data and the last row containing data. Thus, if the last column in which you have data is column F, and the last row in which you have data is row 27, then Ctrl+End will select cell F27.
To do this same task from a macro, you use a very simple command, as shown here:
Sub FindLast1() ActiveCell.SpecialCells(xlLastCell).Select End Sub
This is functionally the same as pressing Ctrl+End. However (and this is a big issue), Excel doesn't dynamically keep track of which rows and columns are the last used in a worksheet. For instance, let's suppose that you open a workbook, press Ctrl+End, and you are taken to cell F27. If you then delete three rows and one column, you would expect that Ctrl+End would take you to cell E24. It doesn't; it still takes you to cell F27 until you save the workbook and reopen it.
This same problem affects the macro code shown in the FindLast1 macro; it will take you to the "highest" cell, regardless of which columns or rows you have deleted during the current session.
What's needed is a way to reset the "last cell" indicator, just as if you had saved and reopened the workbook. There is no intrinsic macro command that does that, but there is a way to force Excel to do the reset. All you need to do is adjust the macro as follows:
Sub FindLast2() x = ActiveSheet.UsedRange.Rows.Count ActiveCell.SpecialCells(xlLastCell).Select End Sub
This macro always takes you to the proper cell—it works as you would expect Ctrl+End to always work. It works because apparently Excel, when it calculates the Count property for the number of rows in the worksheet, always resets the "last cell" indicator.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11526) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Finding the Last-Used Cell in a Macro.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Logical structures are important in programming, as they allow you to control how the programming statements are ...Discover More
The undo list can be a lifesaver when working in a macro. Unfortunately, the undo list is not preserved when you run a ...Discover More
Do your macros seem to be disabled on your new machine? It could be because of the security settings in Excel. Here's ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.