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

by Allen Wyatt
(last updated November 14, 2015)

10

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, and 2016. 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

Auto-incrementing Form Fields

Templates are a great way to create new documents because they act as intricate patterns to what those new documents should ...

Discover More

Turning Off Dynamic Menus

You may want to adjust the way that Excel displays its various menus. This tip explains how you can turn off the dynamic menu ...

Discover More

Deleting Styles

As documents evolve, so do your needs for various styles. You may create new ones and, invariably, old ones need to be ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Develop Macros in Their Own Workbook

If you develop macros and edit them quite a bit, you may be running the risk of causing problems with the macros or with your ...

Discover More

Maximum Length Limit for a Macro

Make your macros too long, and Excel may just refuse to run them at all. This tip explains what the limit is for macros, and ...

Discover More

Determining an Integer Value

When creating macros, you often need to process numbers in various ways. VBA allows you to convert a numeric value to an ...

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

2015-11-16 12:55:15

Scott Renz

I just put the command:

ActiveSheet.UsedRange

before anywhere that I need to know what the last row or column is such as:

ActiveSheet.UsedRange
LastRow = Cells.Specialcells(xllastcell).row


2015-11-16 06:04:40

Barry

@Andrew

The followiong will return the last value:

Function LastCellinColumn(Col As String) As String

LastCellinColumn = Range(Col & Rows.Count).End(xlUp).Value

End Function

You might want to check out the non macro solutions in this tip: http://excelribbon.tips.net/T011030_Referencing_the_Last_Cell_in_a_Column.html


2015-11-16 05:36:30

DaveS

@AM
You can determine the last populated row in column lCol using:

Activesheet.Cells(Row.Count, lCol).End(xlUp).Row

This works with columns containing values; from memory, there may be complications in a column containing formulas which can return blank.


2015-11-16 05:30:20

Barry

@Andrew,

There's several ways to do this dpending a little on how you want to specify the column and what the result is. The following is a 'function' with the column label passed as a parameter (e.g. "A") and returns the address as a string:

Function LastCellinColumn(Col As String) As String

LastCellinColumn = Range(Col & Rows.Count).End(xlUp).Address

End Function


2015-11-16 03:26:07

Andrew McQuillen

Is there a way to modify this macro to find the last cell in a particular column.
say I have some data produced and it is in varying lengths dependent on how long the test ran for. but I want to know what the value of the last piece of data was, compared with the first.
How could I get the last piece of data without reordering the columns.

Thanks
Andrew


2015-11-15 23:19:32

Alex B

I think you will find that the statement

ActiveSheet.UsedRange

will reset the "last cell" indicator.

You can try it by
deleting a few rows and
testing Ctrl+End
then running the command in the immediate window
and then try Ctrl+End again.




2015-11-14 08:10:10

Willy Vanhaelen

Here is a one liner of the second macro:

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


2015-11-14 06:51:18

Matthew

I dig this approach and I'm going to play with it for dynamic reporting workbooks. Right now I'm creating individual sheets and storing counts in variables so that I can produce an "overview" sheet at the end _and_ show where I got the numbers (individual sheets).


2015-11-14 06:46:33

Matthew

I get around the "visible cells" hitch by copying everything to a new sheet (with the filter turned-on) in the macro. It's a little slower and not as slick in the code, but I'm dealing with smaller amounts of flat text multiple times a day.

In this application I find it faster because the filters are permanent changes and if I need "lastRow" I use it and if I need "lastCol" I use that.

Maybe I'm wrong, but I can knock out macro solutions to "one-offs" quickly.


2015-11-14 06:38:01

Matthew

I actually (re-)calculate two variables "lastRow" and "lastCol" before I use them in formulas. (lastRow much more often than lastCol.) I found that this is more flexible and it always returns an integer.


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.