Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timestamp cell change??!! no undo? 2

Status
Not open for further replies.

VulcanJedi

Technical User
Oct 2, 2002
430
US
An associate wants to datestamp any changes made to certain data....I have wrote the following:
This works, but disables the UNDO command, obviously risking data integrity any insights? When does the UNDO command get disabled?


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 11 Then
Target.Next = Date
End If



End Sub [yinyang] Tranpkp [pc2]
 
Hi,

UNDO is NOT off. Its just that UNDO only works for stuff thats NOT done in code -- stuff that the user does DIRECTLY.

The DATASTAMP is not done DIRECTLY by the user.

Hope this helps :) Skip,
Skip@TheOfficeExperts.com
 
You can write your own undo handler, although I haven't tried to use it in combination with an event handler. An interesting proposition...
Rob
[flowerface]
 
I was thinking of that but ran into some philosphical obstacles Rob! ;) [yinyang] Tranpkp [pc2]
 
Yes, if you store the target.value from the Worksheet_SelectionChange event, which occurs just before a worksheet_Change might occur, a SpecialUndo programmer defined button could replace the stored value (a one-step UNDO) Skip,
Skip@TheOfficeExperts.com
 
I played around with this a little bit, and came up with the following:

-- in the worksheet code module --
Private Sub Worksheet_Change(ByVal Target As Range)
Set StampCell = Target.Offset(0, 1)
OldStampCellValue = StampCell
Application.EnableEvents = False
Target.Offset(0, 1) = Date
Application.EnableEvents = True
Application.OnUndo "Undo timestamp", "unstamp"
End Sub

-- in a general code module --
Public StampCell As Range
Public OldStampCellValue
Sub unstamp()
Application.EnableEvents = False
StampCell = OldStampCellValue
Application.EnableEvents = True
End Sub

This works. That is, it puts the correct entry in the edit menu, which, when selected, undoes what the timestamp event does automatically. Unfortunately, once it's done that, it doesn't allow the previous undo, so it's no use at all. Oddly, it seems like the unstamp macro itself (because it has no .onundo method) disables the undo stack. Since this would be counter to any useful undo functionality, I must be missing something. Unfortunately, I don't have any Excel VBA mega-handbook with examples that could set me straight. Luckily, I have Tek-Tips! What's wrong with the code above?
Rob
[flowerface]
 
How were you able to implement such an approach? [yinyang] Tranpkp [pc2]
 
With Excel you should be able to do multiple undo's. I can make it work for the first one (from the change event), but can't get back to the previous ones (which, realistically, are the more important ones, corresponding to real user entries). It should be easy, but I couldn't find a workaround. Must admit I haven't used OnUndo much in my own work.
Rob
[flowerface]
 
Nor I Rob, but great to be aware of it now. It just doesn't seem to capture when an existing value is changed.... [yinyang] Tranpkp [pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top