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: Identifying the Last Cell Changed in a Worksheet.

Identifying the Last Cell Changed in a Worksheet

by Allen Wyatt
(last updated May 27, 2019)

3

John wonders if there is a way in VBA to identify the last cell that was changed by a user. He doesn't want to know if the cell was changed by a macro, but specifically by a user.

The answer is yes—sort of. You can use the Worksheet_Change event to write a handler that will record when any particular cell in a worksheet is changed. A macro that does this could be rather simple, such as this one:

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.StatusBar = Target.Address
End Sub

The macro simply puts the address of the last change into the status bar. You could modify the macro so that it maintained the address in a global variable (declared outside of the event handler) in this manner:

Dim sAddr As String

Private Sub Worksheet_Change(ByVal Target As Range)
    sAddr = Target.Address(False, False)
End Sub

You then could use a regular macro to retrieve the address stored in the sAddr variable and do whatever you want with it.

As for making sure that the event handler doesn't record any changes done by macros, the only way to do this is to turn off event handling before executing any macro command that will modify the worksheet. For instance, the following EnableEvents property change could be used before and after a command that changes the contents of cell A1:

Application.EnableEvents = False
Range("A1") = "Hello"
Application.EnableEvents = True

With event handling turned off, the Worksheet_Change event handler won't be triggered and the "last changed" address won't be updated. The result is that you end up tracking only those changes done by users, not changes done by macros.

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 (11475) 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: Identifying the Last Cell Changed in a Worksheet.

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

Generating a Power Efficiency Diagnostics Report

Your computer uses power to operate, and Windows has a great impact on how much power it uses. You can use a hidden ...

Discover More

Using Mandatory Form Fields

When using form fields to gather information from users of your documents, you may want to make sure that some of the ...

Discover More

Reference to a Range of Endnotes

When multiple endnote references are used at a given point in your document, you may wonder if there is a way to compress ...

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 (ribbon)

Creating a Copy without Formulas

Excel makes copying worksheets (duplicating them) rather easy. However, you may want a worksheet copy that differs from ...

Discover More

Quickly Inserting a New Worksheet

Want a quick way to insert a worksheet? There's nothing faster than using the handy shortcut.

Discover More

Quickly Copying Worksheets

Excel provides a little-known way to copy worksheets simply by clicking and dragging. Here's how to do it.

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

2019-05-29 09:45:53

Alan Elston

@ Peg Molter
So further to possibly answer your specific question.
Youmight be familiar with this situation: If you have your cursor in many normal routines and choose either F5 or hit the play button, then that routine would start. This is because VBA assumes / geusses, that you want to run the routine you are "in"
The sort of event routines Allen Wyatt is discussing are not recognised as a normal routine. VBA does not see it as a normal routine. So if you choose either F5 or hit the play button when selecting such an event routine , VBA will not see any routine. It does not know what routine you want to run. So it will ask you for one.
So that might answer your specific question

Alan


2019-05-29 09:27:18

Alan Elston

Hello Peg Molter,
The routines discussed by Allen Wyatt here are of the type sometimes referred to as “event type routines”
These will typically run automatically when some specific “event” occurs.
The specific routines used here will start automatically when a cell value is changed.

Important to note however, is that the routine must be copied to a Worksheet code module, specifically to the worksheet code module of the worksheet to which you want the routine to react to a change in a cell value.

For example, if you want the routine to react to changes in the second worksheet, then right click on the tab of the second worksheet, then select an option like “view code”, and then paste the coding into the large code window, which usually is empty.
https://www.contextures.com/xlvba01.html#Worksheet

Alan Elston


2019-05-28 08:14:33

Peg Molter

When I run this code:

Private Sub Worksheet_Change(ByVal Target As Range)
Application.StatusBar = Target.Address
End Sub

I get a dialog box asking me for the macro name. Why?


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.