Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Undo & ErrorHandler

Excel 2002, Win XP
I have noticed in these NGs that when an Undo command is issued (in
code) that an Error Handler routine is often used. That is, a statement
like:
On Error GoTo ErrHandler
is put before the Undo command.
Or something like:
If Sheets(xlsName).CanUndo Then.....
I can understand doing this if the Undo capability is in question.
My question regards the need for the above in the specific situation where
the code containing the Undo command is triggered by a Worksheet_Change
event. When and why would the Undo not be available in this situation?
Thanks for your help. Otto


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Undo & ErrorHandler

Undo is almost always at risk in a macro. Many macro commands clear the
undo history.

I rarely see undo code in this workgroup and I don't recall seeing it
encased in error handlers as a noteworthy thing.

If you do it immediately in a change event, it should work. I did a search
on canundo in the newsgroup and your cite for it is the only one I found.
As far as I know, there no such command as canundo. As to my use of an
error handler it is to ensure events are enabled if an error does occur
regardless of the source. Has nothing to do with undo specifically.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count 1 Then Exit Sub
On Error GoTo ErrHandler
If Not Intersect(Target, Range("B9:F20")) Is Nothing Then
vVal = Target.Value
Application.EnableEvents = False
Application.Undo
vVal1 = Target.Value
res = MsgBox("Do you want to replace " & vVal1 & _
vbNewLine & "with " & vVal, vbQuestion + vbYesNo)
If res = vbYes Then
Target.Value = vVal
End If
End If
ErrHandler:
Application.EnableEvents = True
End Sub


Regards,
Tom Ogilvy


Otto Moehrbach wrote in message
...
Excel 2002, Win XP
I have noticed in these NGs that when an Undo command is issued (in
code) that an Error Handler routine is often used. That is, a statement
like:
On Error GoTo ErrHandler
is put before the Undo command.
Or something like:
If Sheets(xlsName).CanUndo Then.....
I can understand doing this if the Undo capability is in question.
My question regards the need for the above in the specific situation where
the code containing the Undo command is triggered by a Worksheet_Change
event. When and why would the Undo not be available in this situation?
Thanks for your help. Otto




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Undo & ErrorHandler

Tom
As always, you took the time to explain and I appreciate that. I now
have a better understanding of why the ErrHandler should be used. The
CanUndo code was in my HowTo file notes and I had to have gotten it from the
NGs. I'll do a quick validation check of it and straighten out my notes.
Thanks again for your help. Otto
"Tom Ogilvy" wrote in message
...
Undo is almost always at risk in a macro. Many macro commands clear the
undo history.

I rarely see undo code in this workgroup and I don't recall seeing it
encased in error handlers as a noteworthy thing.

If you do it immediately in a change event, it should work. I did a

search
on canundo in the newsgroup and your cite for it is the only one I found.
As far as I know, there no such command as canundo. As to my use of an
error handler it is to ensure events are enabled if an error does occur
regardless of the source. Has nothing to do with undo specifically.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count 1 Then Exit Sub
On Error GoTo ErrHandler
If Not Intersect(Target, Range("B9:F20")) Is Nothing Then
vVal = Target.Value
Application.EnableEvents = False
Application.Undo
vVal1 = Target.Value
res = MsgBox("Do you want to replace " & vVal1 & _
vbNewLine & "with " & vVal, vbQuestion + vbYesNo)
If res = vbYes Then
Target.Value = vVal
End If
End If
ErrHandler:
Application.EnableEvents = True
End Sub


Regards,
Tom Ogilvy


Otto Moehrbach wrote in message
...
Excel 2002, Win XP
I have noticed in these NGs that when an Undo command is issued (in
code) that an Error Handler routine is often used. That is, a statement
like:
On Error GoTo ErrHandler
is put before the Undo command.
Or something like:
If Sheets(xlsName).CanUndo Then.....
I can understand doing this if the Undo capability is in question.
My question regards the need for the above in the specific situation

where
the code containing the Undo command is triggered by a Worksheet_Change
event. When and why would the Undo not be available in this situation?
Thanks for your help. Otto






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Instead of one by one Undo karthikbalaguru Excel Discussion (Misc queries) 4 February 5th 09 05:03 PM
Undo Carolyn Excel Discussion (Misc queries) 2 January 12th 07 07:44 PM
can't undo DPW1 Excel Discussion (Misc queries) 4 July 5th 05 08:07 PM
UNDO - how many times can I UNDO? Jane Excel Worksheet Functions 2 May 19th 05 03:03 AM
Why is my undo function in Excel only can undo the last 1 or 2 ch. 1111111111111111111111111111111111111111 Excel Worksheet Functions 1 November 24th 04 11:13 AM


All times are GMT +1. The time now is 08:57 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"