Results 1 to 9 of 9

Thread: VBA Undo - Excel

  1. #1

    Thread Starter
    Member
    Join Date
    May 2004
    Location
    Dubai, United Arab Emirates
    Posts
    33

    VBA Undo - Excel

    Dear Experts,

    Can i please have the code to undo an action in VBA. (EXCEL)

    For example:

    i have run a macro, and i want to undo the action.
    I only need one Undo but Please tell me if more than one undo is possible ?

    The macro to be undone is already available with me, if you could please provide me the code in this structure, would be most appreciated.


    Sub Undo()

    Undo Code here

    my code here. (i will paste my code accordingly)

    End sub..

    Thanks in anticipation,

    S h a n

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Unfortunately running a macro cancels all of the Undo events of Excel.

    You can put your own event on to this list (cant remember how at the moment!), but it must be your own method.. ie: you need a macro which reverses the effect of the first macro.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2004
    Location
    Dubai, United Arab Emirates
    Posts
    33
    Do you mean that the undo macro will vary according to the macro which is to be undone ?

    Is there no common code for this ?

    Best Regards,

    S h a n

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    That's right, if your macro only alters one sheet then you could take a copy of the sheet & hide it (probably in the same workbook), and the "undo macro" would just copy the data back over the original.

    Alternatively you could save a copy of the entire workbook (I'm not sure if this is a viable option - it might cause problems for saving the original later on).

    In either of these cases, it could slow down dramatically if there is lots of data.


    I have never seen any standard code for this, and cant the version I wrote a few years ago either

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    ah-ha! I've found it (suddenly remembered where it was likely to be).

    This is the code to set the Undo command:
    VB Code:
    1. Application.OnUndo "Undo the ZeroRange macro", "undozero"
    Where undozero is the name of the macro to be run which un-does the original macro, and the other bit is the text to show the user.

    If you need more info, the best place is in VBA help

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Cool tip si_the_geek!
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    cheers

    It's always nice to remember something from a few years ago that actually worked.

    It's just a shame that VBA is far too complex for the Undo macro to be created automatically (and you cant keep Undo's from before the macro)

  8. #8

    Thread Starter
    Member
    Join Date
    May 2004
    Location
    Dubai, United Arab Emirates
    Posts
    33
    Hi,

    I could not help my self achieving the code given by you to undo my macro, Considering the below is my macro, could you please amend it to allow undo.

    Overview of the below Code:

    This macro is attached to another macro which is for making new sheets. once sheets are created, you can now double click any row, a userform appears asking where you want to transfer this row (to which sheet) & (to which location). I want when it is double clicked and the row is sent to the created sheet, it should allow undo of the transferred row.


    Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    If Sh.CustomProperties.Count = 0 Then frmTransfer.Show
    End Sub

    ---------------------------------------------------------------------------------
    Option Explicit

    Private Sub cmdCopy_click_Click()
    Dim i As Long, strSheetName As String, shtOriginalSheet As Worksheet

    If ListBox1.ListIndex = -1 Then
    MsgBox "You have to select an item in the list"
    Exit Sub
    End If

    strSheetName = ListBox1.List(ListBox1.ListIndex)

    Select Case MsgBox("Do you want to transfer this row after the Last Used Row?", vbYesNoCancel)

    Case vbYes

    i = Worksheets(strSheetName).Range("d65536").End(xlUp).Row + 1


    Case vbNo
    Set shtOriginalSheet = ActiveSheet
    Application.ScreenUpdating = False
    Worksheets(strSheetName).Activate
    i = ActiveCell.Row
    shtOriginalSheet.Activate
    Set shtOriginalSheet = Nothing
    Application.ScreenUpdating = True

    Case vbCancel
    Unload Me
    Exit Sub

    End Select

    ActiveSheet.Rows(ActiveCell.Row).EntireRow.Copy Destination:=Worksheets(strSheetName).Rows(i)

    Unload Me

    End Sub

    ------------------------------------------------------------------------------

    Private Sub UserForm_Initialize()
    Dim sht As Worksheet
    For Each sht In ThisWorkbook.Worksheets
    If sht.CustomProperties.Count > 0 Then
    If sht.CustomProperties.Item(1).Name = "Sheet" Then Me.ListBox1.AddItem (sht.Name)
    End If
    Next
    End Sub




    Best Regards,

    S h a n
    Last edited by Just Shan; May 5th, 2004 at 04:21 AM.

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    First of all, when you post code on this site, please use the VBCode tags (or the VB button above the typing area), it makes it much easier to read!

    ok.. from what I can see you are basically pasting into the range
    Worksheets(strSheetName).Rows(i) (where strSheetName and i are set earlier in the sub).

    In order to undo this action you need to store the values of strSheetName and i. The code you have over-writes the existing row in the target location, so I guess you will want to store that too (but I am not sure how you would do it).

    As the form is unloaded as part of your macro, you cannot put the Undo code (or variables) in the form, so you will need to put it in a module instead.
    VB Code:
    1. '** Code for the module:  **
    2. Public undo_SheetName As String
    3. Public undo_Row As Long
    4.  
    5. Sub undomacro()
    6. 'Undo the effects of cmdCopy_click_Click
    7.  
    8.   If (undo_SheetName = "") Or (undo_Row < 1 Or undo_Row > 65536) Then
    9.     MsgBox "Sorry, couldnt find the pasted row.", vbExclamation
    10.   Else
    11.             'We have valid sheet/row, so undo!
    12.     Worksheets(undo_SheetName).Rows(undo_Row).Clear
    13.     'Instead of a .Clear you could reset the old values (if you store them in your macro!)
    14.   End If
    15.  
    16. End Sub
    17.  
    18.  
    19. '** Changes to your macro:  **
    20. ...
    21.   End Select
    22.  
    23.      'Store undo info
    24.   undo_SheetName = strSheetName
    25.   undo_Row = i
    26.   'you could (somehow) store the current values of the target row here
    27.  
    28.   ActiveSheet.Rows(ActiveCell.Row).EntireRow.Copy Destination:=Worksheets(strSheetName).Rows(i)
    29.  
    30.      'enable Undo
    31.    Application.OnUndo "Undo the macro", "undomacro"
    32.  
    33.   Unload Me
    34. ...

Posting Permissions

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



Click Here to Expand Forum to Full Width