Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later 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 August 5, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


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. What the shortcut does is to choose 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 3 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.

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 (2271) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

MRU Files Won't Display

Excel provides a quick way to access the workbooks you've most recently worked on. This tip addresses how to display a ...

Discover More

Printing without Footnotes

Want to print your document without all those footnotes included? It's not quite as easy as you might think, as this tip ...

Discover More

Inserting Worksheet Values with a Macro

Macros are often used to process information in a worksheet. You may need your macro to change the values stored in ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Combinations for Members in Meetings

Got a large group of people listed in a worksheet and you want to make sure that each person has met with every other ...

Discover More

Getting a File Name

Does your macro need to allow the user to specify a particular file name that should be used by the macro? Here's a quick ...

Discover More

Counting Shaded Cells

Ever want to know how many cells in a worksheet (or a selection) are shaded in some way? You can create a handy little ...

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 2 + 8?

2018-04-13 15:24:36

Jordan

This doesn't actually work in Excel 2016.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.