MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 23rd, 2002, 08:36 AM   #1
stone
Board Regular
 
Join Date: Apr 2002
Location: iceland
Posts: 138
Default


Is it possiple to undo a macro.

I have created a button in my worksheet,if you push it a userform pops up, this userform has two buttons, if you push button1 then it clear contents in some cells and then the userform closes, if you push button2 then the userform closes.

see code below....

Private Sub button1_click()
Dim r1 As Range, r2 As Range, r3 As Range, myMultiAreaRange As Range
Worksheets("sheet1").Activate
Set r1 = Range("B13:H111")
Set r2 = Range("K13:N111")
Set r3 = Range("P13:S111")
Set myMultiAreaRange = Union(r1, r2, r3)
myMultiAreaRange.ClearContents
userform.Hide
End Sub

Private Sub button2_Click()
userform.Hide
End Sub


What i need to do is to make a another button on my worksheet, that undo the last action that was made, including the userform macro (see code above)(that is it must be able to undo the macro, if that was the last action)

Is this possible to do?

BK
Stulli
stone is offline   Reply With Quote
Old May 23rd, 2002, 08:47 AM   #2
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

There is no builtin method of Undoing the actions of a macro.

It is possible to workaround this. What you will need to do before the macro is run is to make a copy of the workbook in a temp folder or something. Then, when you run the "Undo" macro you simply need to load the copy of the folder, thus giving the illusion of undoing the last action.

(I say "simply", but I would have a search for this sort of thing on this site or even do a web search to see if anyone's got a nice example of this)

HTH


__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old May 23rd, 2002, 08:48 AM   #3
WayneTN
Board Regular
 
WayneTN's Avatar
 
Join Date: May 2002
Location: Tennessee, USA
Posts: 72
Default

Stone,

This won't be much help since I don't use VBA that much, but I have some macro buttons on a worksheet that I update daily. Some of my code does a time calculation and tells me how many times to press one of the buttons (I know, crude, but it works). In case I or someone else presses the update button too many times - planning for everything - I actually have an Un-update button that reverses the last update. This is real simple, but somewhat related. Hopefully someone with more experience can tell you how to do this correctly......Wayne
WayneTN is offline   Reply With Quote
Old May 23rd, 2002, 09:14 AM   #4
Ricky Morris
Board Regular
 
Join Date: Mar 2002
Posts: 363
Default

What you need to do is store the value of the cells that are cleared by the button as soon as the form is loaded. Then if the Undo button is pressed, restore the contents you saved. If you need any further assistance let me know.

You may not need to get as complicated as this but here's an example of how I undo formatting that I have attached to custom buttons on my toolbar.

Type SaveRange
Underline As Variant
Bold As Boolean
Addr As String
Style As Variant
H_Alignment As Variant
Val As Variant
Num_Format As Variant
End Type
Sub CenterAcrossSelection()
' this code is executed when the Center Across Selection
' button is pressed.
On Error Resume Next
Call Save_Undo_Info
Selection.HorizontalAlignment = xlCenterAcrossSelection
Application.OnUndo "Center Alignment", "UndoMyMacro"
' The first argument "Center Alignment" will be displayed
' in the drop down list of the undo button.
' The second argument is the name of the Macro that will
' undo the changes.
End Sub
Sub UndoMyMacro()
On Error GoTo ErrorHanlder
Dim i As Integer
Application.ScreenUpdating = False
OldWorkbook.Activate
OldSheet.Activate
For i = 1 To UBound(OldSelection)
With Range(OldSelection(i).Addr)
.Font.Underline = OldSelection(i).Underline
.Font.Bold = OldSelection(i).Bold
.Style = OldSelection(i).Style
.HorizontalAlignment = OldSelection(i).H_Alignment
.Formula = OldSelection(i).Val
.NumberFormat = OldSelection(i).Num_Format
End With
Next i
Exit Sub
ErrorHanlder:
MsgBox "Can't Undo", vbCritical
End Sub
Sub Save_Undo_Info()
Dim cell As Range
Dim i As Integer
If TypeName(Selection) <> "Range" Then Exit Sub
ReDim OldSelection(Selection.Count)
Set OldWorkbook = ActiveWorkbook
Set OldSheet = ActiveSheet
i = 0
For Each cell In Selection
i = i + 1
With OldSelection(i)
.Addr = cell.Address
.Underline = cell.Font.Underline
.Bold = cell.Font.Bold
.Style = cell.Style
.H_Alignment = cell.HorizontalAlignment
.Val = cell.Formula
.Num_Format = cell.NumberFormat
End With
Next cell
End Sub

__________________
It's never too late to learn something new.

Ricky
Ricky Morris is offline   Reply With Quote
Old May 23rd, 2002, 09:18 AM   #5
stone
Board Regular
 
Join Date: Apr 2002
Location: iceland
Posts: 138
Default

Thanx alot guys.

BK
Stulli
stone is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 07:10 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes