Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. 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.

Finding the Last-Used Cell in a Macro

Written by Allen Wyatt (last updated February 5, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

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. You can also rely on the UsedRange object in a different manner:

Sub FindLast3()
    ActiveSheet.UsedRange.SpecialCells(xlLastCell).Select
End Sub

The result, again, is that the desired last cell is selected.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11526) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Finding the Last-Used Cell in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Deleting Blank Rows

Got some pesky blank rows in your data that you want to get rid of? This tip provides a wide variety of methods you can ...

Discover More

Retrieving Worksheet Names

Want to grab the names of all the worksheets in a workbook? Here's how you can stuff all those names into the cells of a ...

Discover More

Specifying a Default Building Block Location

When you create a Building Block in Word, it is saved in a particular location by default. If you want to change that ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Showing RGB Colors in a Cell

Excel allows you to specify the RGB (red, green, and blue) value for any color used in a cell. Here's a quick way to see ...

Discover More

Selecting a Specific Cell in a Macro

Need to use a macro to select a specific cell in a different workbook? It's not as straightforward of a proposition as ...

Discover More

Converting Strings to Numbers

When working with data in a macro, there are two broad categories you can manipulate: numbers and text. Sometimes you ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 + 5?

2022-02-05 09:55:28

J. Woolley

The Range.SpecialCells method does not work correctly when referenced directly or indirectly in a user defined function (UDF). For more on this subject, see https://www.excelanytime.com/excel/index.php?option=com_content&view=article&id=86:last-used-row-last-used-column-vba&catid=79&Itemid=475#Use%20End(xlUp)


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.