I am wondering if I can undo all the actions in the undo queue,
I know of the Application.Undo function but this only undoes the last action and clears the queue, I tried putting this in a loop and it just kept undoing then redoing.
Extra info:
Users open a form in read only,
Accidentally change fields then use a form that makes changes then saves it (in vba) and the accidental changes are saved too,
I want to undo all the accidental changes before the user runs the form so only the necessary changes are saved.
Can I undo multiple actions at once?
You can close your workbook without saving. This is the only method I am aware of. To avoid data loss run your macros on the copy of your data.
Basically, you can't undo anything after VBA runs...
Related
I have an Excel workbook macro that opens another workbook and starts copying data into it and formatting it. At one point in the process, I want the macro to pause and let the user manually enter data into the target workbook and then resume processing.
MsgBox, Application.Wait(), and Sleep are all application modal and will not let the user update anything in the other workbook while they are executing.
I found this while searching for a solution. It gets me halfway there in that I can manipulate the other sheet but only with my mouse. No keyboard presses get sent to the workbook.
Any ideas on getting all the way there?
I was thinking that I could just have two macros. The user would run one, then perform his manual tasks, then run the other. This appears to work but I would have to convert everything to globals so hopefully, someone has a better idea.
Thanks!
Depending on the macro being run to copy and paste, is the main concern with user intervention during execution of the macro getting the active cell/sheet (if being used) back to being active after the user manipulates something.
I'd recommend storing the active cell/sheet address in a variable prior to the Application.Wait() and then setting the active cell to that stored value on resume.
Without a posting of what your macro is doing though, it is hard to know if this suggestion helps your current situation.
There are a half-dozen answers to this. "Open a second instance" "Have a pause" Etc. I'm not looking for that.
I'm looking for the user of the workbook to be able to manipulate the workbook while the macro is running. I've seen this working before, where the user could scroll around, change tabs, even add and remove data, all while the macro was running. Unfortunately, I couldn't get permission to look at the code (And committing CFAA violations ins't my cup of tea), so I have no idea how they did it.
How can you enable a user to edit the workbook as macros are running? For a specific example, I have Conway's Game of Life running. Users select cells to flip live/dead, then can run a macro to run the entire thing. I think it'd be nice for users to be able to change cells as the macro is running. (which is a second on select macro)
Thank you
Sorry just reread the question. I wouldn't expect the permutation to run for very long - not long enough to interrupt really.
But if it does, then the advice about using lots of DoEvents stands.
The other option is that you can use the OnTime event to have a "heartbeat"
VBA Macro On Timer style to run code every set number of seconds, i.e. 120 seconds
You can set the timer to say 3 seconds. Every time the OnTime event occurs you do one step of your permutation. In the three seconds in between they can edit.
Refactor your macro to use Events. In which case, you would have a series of event handlers (instead of one monolithic macro) to respond to various triggers. This is assuming that the macro is influenced by what the user is doing in the worksheet.
One way of (sort of) doing this is to use a Modeless Userform (UserForm.Show vbModeless)
The user form stays visible but the VBA stops running when the form is shown and the user can then interact with Excel. Then when the user clicks a button on the form the code behind the button starts running again.
So in reality the user is either interacting with Excel or interacting with the form ...
This question already has answers here:
Building Undo Into an Excel VBA Macro
(3 answers)
Closed last year.
I want to know if we can undo the macro action by any chance. I am using the excel sheet as a form and I am having a submit button(Macro) which takes the sum of counts of the sheet(based on the form input) and stores it in the next sheet.
My problem is, if we press the submit button without completing it or if we press it twice, the sum which I store in the next sheet, becomes inaccurate. If there a way we can undo the macro actions in excel? I tried using the undo button, but it didn't work for macros. Is there a way we can undo it?
Can we add another macro which would undo the previous macro's work?
I agree with all the commenters who've suggested that the best practice is to validate the starting conditions and/or input values before allowing the macro to make any changes. However, validation is often very complex and totally impractical in a lot of "we need it now" situations.
Here are two very easy things I can suggest:
1) Have the macro save the workbook before any changes are made, but not save the workbook after the changes have been made. This way, if you see something went wrong, you can just close and reopen the workbook and you'll be back to where you were before the macro did whatever the macro does.
2) Have the macro save copies of any affected worksheets before taking any action, so, if things go wrong, you can revert (or create a macro to revert) back to the starting point.
The first option requires the least amount of code, just:
ThisWorkbook.Save
before letting the macro do whatever the macro does.
I frequently use this method when testing macros.
The second option is a little more complex, but not much:
ThisWorkbook.Worksheets("YourWorksheet").Copy(After:=ThisWorkbook.Worksheets("NameOfSheetYouWantItToAppearAfter")
Note that this will activate the copy. If necessary, you can reactivate the original worksheet like this:
ThisWorkbook.Worksheets("OriginalWorksheet").Activate
I hope that helps!
The Workbook.Save line in my macro is holding everything up, and while it's important that there's a save step at the end of the macro, I don't mind if it just starts saving and then hands control back to the user.
Is there such a thing as Workbook.Save BackGround or Workbook.Save vbModeLess?
Is there such a thing as Workbook.Save BackGround or Workbook.Save vbModeLess?
Definitively, no. The full list of methods available to the workbook object:
http://msdn.microsoft.com/en-us/library/office/ff847316(v=office.14).aspx
The .Save method does not have any optional arguments:
http://msdn.microsoft.com/en-us/library/office/ff197585(v=office.14).aspx
It seems you are perceiving a "problem" with your code which is not actually a problem, but normal and expected functionality, as I explained in the comments above:
When a user manually saves the file, the application is not interactive. The user can't do anything except wait for the save to finish.
The same occurs when you invoke the .Save method of the workbook object, or the Application.CommandBars.ExecuteMso "FileSave", etc.
This is necessary because (obviously) changes made while saving would not be saved, but the workbook's .Saved property would display True.
This property is used in determining whether to show the "Close this workbook with unsaved changes?" dialog when the user closes the file. If the property is True, then the user can close without any prompt. But of course if you let them make changes this will inevitably lead to unwanted data loss as the user may then close the file with saved state True and unsaved changes to the workbook which have not been reflected in the Saved property.
(Note: there are probably more technical reasons, too, but this is just the common-sense explanation)
If the length of time it takes to save the file is burdensome, you have at least a few options I can think of, first you would want to consider notifying the user that the file is going to be saved and this may take upwards of 45 seconds. This way, they do not think the program is unresponsive or otherwise hanging. You can do this with a MsgBox or a UserForm pretty easily.
Alternatively, you could use either of the above methods to prompt the user, i.e., "Do you want to save the file?"
Etc.
When excel saves a file it created a temporary file with a name like A82732KS.tmp and the quickly delete the original file and rename the temp file (possibly in an atomic operation). To do this excel has to release control of the file to avoid a sharing violation so it necessarily disables any changes in memory in order to guarantee that was is written on file and what is loaded in memory is identical.
When you run a VBA macro under Excel, it erases the undo history. Is there any way to prevent this? I'd like to capture the undo history before running my macro, and restore it after my macro is finished. I didn't notice anything in the Excel documentation that looked promising.
There is no way to keep undo history in an Excel macro (there is in a Word macro, so VBA itself has the hooks, which makes it a confusing business). Whenever the VBA launches, Excel kills the undo stack.