I have a complex project in `Microsoft Office Excel 2007' which utilises a large number of UDFs. Through VBA in a Workbook_Open Event, I set Excel Automatic calculation to OFF and a strategically placed Calculate method to manually calculate the cells whenever I need it so that the UDF doesn't perform recalculation unintentionally.
If the workbook is the ONLY one opened (or the first to open) in an Excel instance, everything works perfect. Only when it's opened AFTER another workbook (within the same instance), my project will inherit the Automatic calculation setting from the FIRST workbook and perform endless calculation on my UDFs. The disable code placed in the Workbook_Open event isn't executed until the UDF finishes the calculation (which can take forever). This only happens if my project is NOT the one opened first.
Through http://www.decisionmodels.com/calcsecretse.htm, I discover that it is the nature of Excel to perform the calculation process BEFORE the Workbook_Open event is executed.
So the question I have obviously relates to the project being opened AFTER another workbook is opened with automatic calculation turned ON:
How do I force my project to disable automatic calculation
without it performing recalculation first (remember, problem only
occurs when the project not the first one to be opened since it will
follow settings from previously opened workbook) OR...
How do I get the project to open in ANOTHER INSTANCE (when double clicked) to avoid
inheriting automatic calculation setting from the previous workbook.
Either way, the answer I'm seeking is for the project to open without performing the calculation first.
Thanks
One way is to use a different workbook (Opener.xls) to initiate opening the UDF workbook (udf.xls)
in Opener.xls the Workbook_Open code
- sets calculation to manual
- opens udf.xls
In your question I don't recognize the way you use to change and inherit that option to your workbooks, But I answer it as a solution:
Use VBA and running VBA macros to change that option for just your active sheet as soon as you need to calculate; by using it like this:
With ActiveSheet
.EnableCalculation = False
.EnableCalculation = True
.Calculate
End With
In another ways that may you need, you can read this part of MSDN article.
Related
I would like a global solution every time I start Excel365 with my key add-in also firing up.
I defined several LAMBDA custom functions, saved them in my add-in, and I would like to make them available globally - not just only for the current workbook. Is it possible? I tried saving a workbook with definition as an add-in (.xlam) but the formulas are not available. The don't seem to be available when as an add-in. When I am in the add-in parent file on the desktop, all is good and everything is running. But once I post the file back for your as an add-in, The Lambda(s) and the defined name space go away.
You might try creating a template for excel to load when it is opened.
Customizing how Excel starts so it utilized the template.
My additional work around is also simple. If the LAMBDAs were saved in the original Workbook with "Workbook" as the saved parameter. Then each of those worksheets in the original LAMBDA workbook is "infected" with all of the workbook LAMBDAs. Therefore, I simply copy a blank worksheet from the original Workbook to the "new" workbook. Then by virtue of "infection" the new workbook is also newly infected with all of the new LAMBDAs. It works just like COVID, who knew?
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.
I have various routines in two different modules. Some clears the report, some populates it, fill the blank spaces with zeroes. I am currently running them using F5. But I want when the user opens the sheet, he gets all populated data without having to run that particular sub routine. Is it possible to run the routines in various modules automatically when the excel is opened?
This is pretty easy. Instead of putting code in the module, you will put it in ThisWorkbook and use the Workbook_Open event. You don't necessarily have to move your code. You can just call the existing macros.
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!
I've got this Design Template I want to apply to the application by default whenever it's opened. But a document needs to be open in order to apply a Design template. So I have to target some handlers whenever a new or existing workbook is opened/created.
So which approach can I take here?
I think you can use the Workbook_open event. It sounded like that would be sufficient for the behavior that you want, but since you want your macros to be global, you might need to make them into an add in for them.
The add-in approach is best for applying to existing workbooks.
If you're looking to have a standard design template applied to each new workbook, though, I suggest you create a template.
To do this:
Create a new empty workbook
Apply the design template
Save the file as Book.xlt (for Excel 2003; Book.xltx for versions 2007, 2010) in the following folder: %appdata%\Microsoft\Excel\XLSTART\
Each time you create a new workbook (by opening Excel or hitting the "New" button), it'll be generated from this template file.