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 Mar 31st, 2002, 06:59 AM   #1
dan2
Board Regular
 
Join Date: Mar 2002
Posts: 60
Default

I seem to be full of questions today

How do I put a button on my spreadsheet that will do the same function as the undo button on the tool bar?
(the tool bar is hidden from the end user)

Thanks in advance (again)

Dan
dan2 is offline   Reply With Quote
Old Mar 31st, 2002, 07:12 AM   #2
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Hi Dan,

If you make the Control Toolbox visible (View, Toolbars) and choose a Command Button). Draw it onto your sheet where you'd like. Right click it and choose Properties. Set the TakeFocus******* property to False. Then double click the button and use this code:-

Private Sub CommandButton1_Click()
Application.Undo
End Sub

You have to set the TakeFocus******* to false so that the worksheet keeps the focus and the undo method won't fail.

HTH,
Dan
dk is offline   Reply With Quote
Old Mar 31st, 2002, 07:25 AM   #3
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi
Now it is working...
Sent E-Mail, could not post.
I could be wrong, but the above will only undo the last user interface action.
If you create a custom Toolbar, add the Tools you wish, and then call it by name as visible, you will retain full functionality for that or those tools.
Tom
Tom Schreiner is offline   Reply With Quote
Old Mar 31st, 2002, 07:56 AM   #4
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Tom,

You're not wrong. The code I provided would not provide the full functionality of the normal Undo button - it'll only undo the last action carried out by the user before the macro was ran. I believe that Undo stores the last 16 actions. This code is an example of what you suggested in that it creates a commandbar with the fully functional Undo button on it.

Regards,
Dan

Code:
Sub AddBar()
Dim comBar As CommandBar

'Delete the commandbar if it already exists
On Error Resume Next
Application.CommandBars("MyBar").Delete
On Error GoTo 0

'Create a floating commandbar with an Undo button on it
Set comBar = Application.CommandBars.Add("MyBar", msoBarFloating)
comBar.Controls.Add ID:=128 '128 is the ID of the Undo button
comBar.Visible = True
End Sub
dk is offline   Reply With Quote
Old Mar 31st, 2002, 08:13 AM   #5
dan2
Board Regular
 
Join Date: Mar 2002
Posts: 60
Default

DK

The command button idea is the one I like for this app. however after testing it I have found 1 problem with it.

The sheets it will be on are part protected and if the user presses the button and there is nothing to undo then it just breaks the code.

Any ideas of how toget round this?

Dan.
dan2 is offline   Reply With Quote
Old Mar 31st, 2002, 10:04 AM   #6
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Quote:
DK

The command button idea is the one I like for this app. however after testing it I have found 1 problem with it.

The sheets it will be on are part protected and if the user presses the button and there is nothing to undo then it just breaks the code.

Any ideas of how toget round this?

Dan.
Hello again Dan,

The way around this is to include error handling. Try this if you want to let the user know that the undo operation has failed:-

Code:
Private Sub CommandButton1_Click()
Err.Clear
On Error Resume Next
Application.Undo
If Err.Number <> 0 Then MsgBox "Cannot undo"
End Sub
or this if you're not worried about them knowing:-

Code:
Private Sub CommandButton1_Click()
On Error Resume Next
Application.Undo
End Sub
HTH,
Dan



[ This Message was edited by: dk on 2002-03-31 09:07 ]
dk is offline   Reply With Quote
Old Apr 1st, 2002, 02:02 PM   #7
duddm00
New Member
 
Join Date: Mar 2002
Posts: 5
Default

Why make things difficult if there is an easier way...just press Ctrl Z.
If you want to go back a few more steps repeat the action.
Melvin.
PS I dont know offhand how many steps back U can take.
duddm00 is offline   Reply With Quote
Old Apr 1st, 2002, 03:34 PM   #8
Jack in the UK
Board Regular
 
Join Date: Feb 2002
Posts: 3,065
Default

Hi guys

OK undos.. if nacro run one way trip no undo .. any fix

Or is it business as usula as you program in you also remenber to program out.... if this is needed of cause,,

What im asking is there a UNDO to VBA actions.. like th eundo button.. i never found one...
__________________
Free Excel based Web Toolbar available here.

Jack in the UK
J & R Excel Solutions
"making Excel work for you"
Jack in the UK is offline   Reply With Quote
Old Apr 1st, 2002, 03:47 PM   #9
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

Jack,

I never found an undo for VBA actions, if I am in a case where I know I will need to do an undo action, then I make a copy of the sheet before the macro runs and then my undo button deletes the old copy and replaces it with the copied version. I know its not a very efficient way, but it works.
__________________
Kind regards,

Al Chara
Al Chara is offline   Reply With Quote
Old Apr 1st, 2002, 03:51 PM   #10
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Jack,

You can't undo a macro using normal Undo. What you can do is write a macro which undoes what the first one did and then use Application.OnUndo e.g.

Paste this code into a module and then run macro1. Now click the Undo button and Macro1 will be undone. If you click the dropdown list next to the Undo button you'll see the text Undo Macro1. This is the first argument of the OnUndo method.

HTH,
Dan.

Code:
Sub Macro1()
'A very simple demonstration of Application.OnUndo
Range("A1:A10").Formula = "=rand()"
Application.OnUndo "Undo Macro1", "UndoMacro1"
End Sub

Sub UndoMacro1()
Range("A1:A10").ClearContents
End Sub
dk 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 11:09 AM.


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