![]() |
Undo Macro Action
Hi,
I need some help with a code to undo an action. The code I have to do an action is below, this sorts cells into order from smallest to largest based on input of hours. Here is the code: Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim strPrompt As String Dim intbuttons As Integer Dim strTitle As String If Target.Address = Range("K2").Address Then strPrompt = "Do you want Put Staff into OT Order?" intbuttons = vbYesNo + vbInformation strTitle = "Galashiels Operational Resources © MN " If MsgBox(strPrompt, intbuttons, strTitle) = vbYes Then Range("A7:D16").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range("C7:C16" _ ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("A7:D16") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("F7:I16").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range("H7:H16" _ ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("F7:I16") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A24:D33").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "C24:C33"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("A24:D33") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("F24:I33").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "H24:H33"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("F24:I33") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A41:D50").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "C41:C50"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("A41:D50") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("F41:I50").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "H41:H50"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("F41:I50") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A1").Select End If End If End Sub What I am needing to do is have another code so that the user can select another cell K4 to undo the changes that have been made by the macro and to also clear contents of cells B3, B4, B5 and C5. Can anyone help me onthis code. Many thanks Mark |
Undo Macro Action
hi
macros skip all of the built in niceities of excel such as undo. a macro does not remember what it did or the values it changed. so undo and macro are like oil and water. not easily mixed but it can be done. see this site. http://spreadsheetpage.com/index.php...ba_subroutine/ good luck. regards FSt1 "terilad" wrote: Hi, I need some help with a code to undo an action. The code I have to do an action is below, this sorts cells into order from smallest to largest based on input of hours. Here is the code: Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim strPrompt As String Dim intbuttons As Integer Dim strTitle As String If Target.Address = Range("K2").Address Then strPrompt = "Do you want Put Staff into OT Order?" intbuttons = vbYesNo + vbInformation strTitle = "Galashiels Operational Resources © MN " If MsgBox(strPrompt, intbuttons, strTitle) = vbYes Then Range("A7:D16").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range("C7:C16" _ ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("A7:D16") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("F7:I16").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range("H7:H16" _ ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("F7:I16") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A24:D33").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "C24:C33"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("A24:D33") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("F24:I33").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "H24:H33"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("F24:I33") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A41:D50").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "C41:C50"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("A41:D50") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("F41:I50").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "H41:H50"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("F41:I50") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A1").Select End If End If End Sub What I am needing to do is have another code so that the user can select another cell K4 to undo the changes that have been made by the macro and to also clear contents of cells B3, B4, B5 and C5. Can anyone help me onthis code. Many thanks Mark |
Undo Macro Action
Hi Mark,
What I have always done when I want to undo a sort is to create a Master Index in an extra column. You can either insert 1 then 2 in the first 2 cells and Aufofill the cells down to the bottom of your data you you can insert =ROW() and Copy down and then Copy the column and Past - Special over top of itself to remove the formulas. The above can either be done as a permanent feature of the worksheet or you can use code to create the Master Index just before the sort. Then all you need to do is include the Master Index column in the sort and to return to the original just sort on the Master Index. You can hide the Master Index column. Following code to clear contents of cells B3, B4, B5 and C5. Range("B3:B5,C5").ClearContents -- Regards, OssieMac |
Undo Macro Action
Close the workbook and don't save it. That'll undo the action you just made.
I know that's probably not what you want, but it has the desired effect. -- HTH, Barb Reinhardt "terilad" wrote: Hi, I need some help with a code to undo an action. The code I have to do an action is below, this sorts cells into order from smallest to largest based on input of hours. Here is the code: Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim strPrompt As String Dim intbuttons As Integer Dim strTitle As String If Target.Address = Range("K2").Address Then strPrompt = "Do you want Put Staff into OT Order?" intbuttons = vbYesNo + vbInformation strTitle = "Galashiels Operational Resources © MN " If MsgBox(strPrompt, intbuttons, strTitle) = vbYes Then Range("A7:D16").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range("C7:C16" _ ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("A7:D16") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("F7:I16").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range("H7:H16" _ ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("F7:I16") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A24:D33").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "C24:C33"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("A24:D33") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("F24:I33").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "H24:H33"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("F24:I33") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A41:D50").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "C41:C50"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("A41:D50") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("F41:I50").Select ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Staff OT").Sort.SortFields.Add Key:=Range( _ "H41:H50"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Staff OT").Sort .SetRange Range("F41:I50") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A1").Select End If End If End Sub What I am needing to do is have another code so that the user can select another cell K4 to undo the changes that have been made by the macro and to also clear contents of cells B3, B4, B5 and C5. Can anyone help me onthis code. Many thanks Mark |
All times are GMT +1. The time now is 11:03 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com