PDA

View Full Version : Enabling "undo" on "time stamp vba code"



aloy78
08-24-2011, 12:54 AM
Hi,
I've search on some sites that "undo" is not enable when the following code is applied:


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
Application.EnableEvents = False
Target.Offset.Offset(0, 1) = Now()
Application.EnableEvents = True
End If
End Sub


It will be better if we can "undo" cause when I accidentally type over on the next cell, I have to not safe my work and redo the whole thing all over again.

Bob Phillips
08-24-2011, 02:05 AM
You would need to program some undo action, maybe have a button to undo the last action, which means saving previous states.

aloy78
08-24-2011, 07:50 PM
You would need to program some undo action, maybe have a button to undo the last action, which means saving previous states.

Hi xld,
Any clue on how to do it? Since "Ctrl + Z" doesn't work when that code is applied :)

Bob Phillips
08-25-2011, 12:54 AM
Yes, as I said, when you change things you need to save their previous state, maybe a 2D array of cell address and value, and then have an undo button that re-applies from within that array. You would have to manage the array, set a limit and shunt things in and out.

rcharters
08-25-2011, 03:48 PM
This code will save the value in column B to the Office Toolbar before overwriting it. You can then access the Office toolbar to paste the value back into the cell.


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
Application.EnableEvents = False
If Not IsEmpty(Target.Offset(0, 1)) Then
Target.Offset(0, 1).Copy
End If
Target.Offset(0, 1) = Now()
Application.EnableEvents = True
End If
End Sub