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

by Allen Wyatt
(last updated September 26, 2017)

5

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.

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

Replacing and Formatting at the Same Time

The Find and Replace feature in Word is very powerful. So powerful, in fact, that you can change the formatting on ...

Discover More

Spell Checking Forms

Word may be used to create protected forms that limit where the user may input data. Normally spell checking is disabled in ...

Discover More

Error in Linked PivotTable Value

Excel allows you to link to values in other workbooks, even if those values are in PivotTables. However, Excel may display an ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

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 macro ...

Discover More

Easily Adding Blank Rows

Want to add a bunch of blank rows to a your data and have those rows interspersed among your existing rows? Here's a quick ...

Discover More

Conditionally Displaying a Message Box

You can, from within your macros, easily display a message box containing a message of your choice. If you want to display ...

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}] 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 four less than 4?

2015-03-09 12:39:20

Willy Vanhaelen

This is a very usefull tip. I included the second macro to my tools collection and assigned it the short-cut key Ctrl+Alt+End. I use that now instead of Ctrl+End.


2013-07-27 04:37:17

Zaied

Hello Philip,

Thanks for the tips.
But I would like to apply last cell with END command to go in the last row of my position cell. Example is,
my macro have to select range of A1 to A450 where CTRL+END going to F450.
I'll not define the range, it will automatically select the range from my current to the last cell of END.

Zaied


2013-06-03 05:37:48

Andrew McQuillen

Thanks Philip,
I will look and see if i can use this to finde the average of the last 16 rows.
i did manage to do this but it was naything but slick so i would like a method which was a bit less clunky!!!
best regards
Andrew


2013-06-01 06:18:11

PhilP

Andrew: I use the following line of code which might be of use to you.

Dim LastRow As Long
LastRow = WorksheetFunction.CountA(Range("A:A")) + 1

The code counts the number of occupied rows in column A and adds 1 to move to the first blank row

In your case change the +1 at the end of the code to -16

The code only works if there are no blank cells in the selected column


2013-05-31 05:50:37

Andrew McQuillen

This is a great tip but i wonder if there is a way to modify it to look for the last cell in the row rather than the last used. ie and using the example above instead of finding f27 as the last occupied cell can you find d27 which is the last row that data has been added into.
I want to use this as i would then like to count back 16 rows and take an average of the last 16 numbers which appear within (again using the example above) d11 to d27.
is this whole project possible?

Thanks

Andrew


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.