Any way to save and recover undo history in VBA? - vba

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.

Related

Only save the code

I'm currently learning Excel VBA and since CTRL+Z doesn't undo anything, I'm trying to find a way to undo what my code does when I have to. On my office computer, I can click on the Save button from VBE and it saves my code, but nit the workbook.
On my personal computer (Office 365), when I do the exact same thing, it saves everything, code and workbook so I cannot undo what my code did by not saving it.
Can you help me to understand why? And do you have any idea I can undo what my code did?
Thank you
You have two options:
1) Export code modules to file - either manually by right-clicking on them, or via code (check out VBA Extensibility), or;
2) Do what I do, which is the easy option of just copying chunks of code as text into Notepad.
There doesn’t exist what you’re really looking for: an integral method of “saving” the VBProject but ignoring the workbook - as the VBProject is part of the workbook when it comes to file structure.

Pause VBA Macro to allow manual input of data

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.

Is it possible to undo a macro action? [duplicate]

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!

Excel not responding when I try to open VBE

I need help, Excel is not responding. I have written a lot of code in VBA and right now I can't even open VBE. I can open the workbook okay, but every time I make any change to one of the cells (I have a lot of code that is triggered by the worksheet_change event based on the cell that was changed) it will not respond. I'm 80% sure it is something wrong with my code, but I can't open the VBE to diagnose because that will also cause Excel to not respond, forcing me to close the application.
I have tried opening Excel in user-initiated safe mode, but this doesn't fix anything.
Any ideas??
Change your Security Settings to prevent macro execution. You can then open file and examine the macros without fear they will execute.
EDIT#1:
Close all files and programs. Then, restore your computer to an earlier time period. You can find out how to do this in 'Control Panel'. This will fix it.

Excel VBA - Call macro using add in

I have added a toolbar menu for my macro, so I can just press the button and it runs my macro from any excel document. Every time I run the macro though, it opens the source file containing the macro. Is there a way that it won't open the source file and just run the macro? (even opening and closing wouldn't too much of an issue, but I'd prefer not opening it at all)
I haven't been able to find any information about this, so all help to get me started is appreciated.
You can't pull code out of the air to run it; Excel's going to have to open the file the code's stored in to run the code.
If I understand it correctly, you can create an Excel add-in from your code and have the add-in load automatically when Excel starts. Your code will always be available in that case.
This seems like a good place to start:
http://msdn.microsoft.com/en-us/library/aa140990(v=office.10).aspx
USE YOUR PERSONAL MACRO WORKBOOK
Open the VBEditor and find the module containing your macro.
Copy that code.
Now in the VBProject Panel on the left, locate your PERSONAL.XLS or PERSONAL.XLSB project. This is the project where you store macros you want available at all times. Add a module to that project and put your code into it.
Now update your "button" so that it points to the macro in that workbook and you're good to go.
On the off chance your PERSONAL.XLS project does not exist, use the macro recorder to record a "junk" macro and be sure to set it to "Store Macro In: Personal Macro Workbook"... that will create it for you the first time.