+ Reply to Thread
Results 1 to 5 of 5

VBA to Undo routine after erroneous entry?

  1. #1
    Registered User
    Join Date
    04-24-2004
    Location
    Wisconsin, USA
    MS-Off Ver
    2007
    Posts
    76

    VBA to Undo routine after erroneous entry?

    Is there a way to trigger an Undo command (Ctrl+Z) via VBA – something like: Application.SendKeys ("^z").

    I have a routine that checks if a condition exists after an entry is made (which updates other cells). If the condition exists a message appears stating you cannot do that. After clicking OK, I want the routine to trigger an Undo (Ctrl + Z) returning all cells to the situation prior to the erroneous entry.

    I can manually click the undo arrow and all works fine. Can I do this via VBA by adding this to the end of the routine after the erroneous message appears which would reset all back to the condition prior to the erroneous entry?

    Thanks,
    Steve

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,246

    Re: VBA to Undo routine after erroneous entry?

    OnUndo method.

    Artik

  3. #3
    Registered User
    Join Date
    04-24-2004
    Location
    Wisconsin, USA
    MS-Off Ver
    2007
    Posts
    76

    Re: VBA to Undo routine after erroneous entry?

    Thank you Artik,

    I am confused as to how exactly the Application.OnUndo is suppose to work.

    I’m still not able to “undo” the action. I am calling a macro (PaymentLimit) in my Worksheet_SelectionChange routine. If the maximum number of payments has been exceeded the macro is triggered which affects other cells. I click on the OK button and press CTRL+z and all reverts back to the status before the erroneous entry which is great.

    Here’s the PaymentLimit –

    Please Login or Register  to view this content.
    I am confused as to where to place the UnDo command and how to be coded. As a test, I created a second macro called UndoLimit. I tried running it via a shortcut key after clicking on the message box OK button but nothing happed.

    Here is the UndoLimit –

    Please Login or Register  to view this content.
    What I was hoping for was simple way to place a Ctrl+Z command after the message box appears but apparently that is not possible. Any other suggestions or assistance would be appreciated.

    Thanks again,
    Steve

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,246

    Re: VBA to Undo routine after erroneous entry?

    That's not how it works. You have to write yourself a procedure that will reverse the effects of the macro that " made a mess" for you. You indicate the name of this reverse procedure in the OnUndo method. Preparing such a procedure is often difficult and sometimes impossible.
    If it is critical for you such a moment, you should check all the conditions beforehand "in memory", without writing to the cells. Only when the data has successfully passed all the tests can you enter it into the cells. The "Undo" stack is cleared when the code action interferes with the structure of the workbook. Any change in the code of a value in a cell, its formatting and others immediately clears the Undo stack.
    Perhaps you should think about creating a form where you enter all the parameters for critical calculations. There you will check all the conditions and, if they are met, only then will you allow the data to be written to the worksheet.

    Artik

  5. #5
    Registered User
    Join Date
    04-24-2004
    Location
    Wisconsin, USA
    MS-Off Ver
    2007
    Posts
    76

    Re: VBA to Undo routine after erroneous entry?

    I understand. I have no idea how to reverse the entry once processed so I guess I will just pass on this . I really don't want to create an entry form because it will actually be quite rare that this is triggered. It sure would have worked easier MS would have included a Sendkeys ("^z") or some similar option. Oh well, now I know.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] naming a routine and calling it in another routine - not a loop
    By HeyInKy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2015, 11:54 AM
  2. Replies: 0
    Last Post: 11-22-2014, 05:02 PM
  3. Pass a variable from one sub-routine to another sub-routine
    By gowtham_pec in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-01-2013, 07:07 PM
  4. Replies: 0
    Last Post: 02-29-2012, 01:40 PM
  5. Undo function to undo Visual Basic commands
    By pierre08 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-15-2010, 04:59 AM
  6. My undo routine fails with drag and drop.
    By serdar in forum Excel General
    Replies: 5
    Last Post: 06-12-2005, 02:05 PM
  7. How to validate and -if invalid- undo an entry?
    By serdar in forum Excel General
    Replies: 1
    Last Post: 06-11-2005, 01:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1