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: Identifying the Last Cell Changed in a Worksheet.
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3819) 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: Identifying the Last Cell Changed in a Worksheet.
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!
Some easy steps to rename the worksheets in your Excel workbook.
Discover MoreExcel allows you to "freeze" rows in your worksheet. What if you want the rows that are frozen to change as you scroll ...
Discover MoreRadio buttons are great for some data collection purposes. They may not be that great for some purposes, however, for the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments