Written by Allen Wyatt (last updated November 7, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Excel allows you to control when it recalculates a worksheet. Normally, Excel recalculates anytime you change something in a cell. If you are working with very large worksheets that have lots of formulas in them, you may want to turn off the automatic recalculation feature. You can turn off automatic recalculation using the tools in the Calculation group on the Formulas tab of the ribbon.
Your macros can also force Excel to recalculate your worksheet. If you have automatic recalculation turned on, then any change your macro makes in a worksheet will force Excel to recalculate. If you have automatic recalculation turned off, then you can use the Calculate method to recalculate a worksheet:
ActiveSheet.Calculate
Of course, if recalculation takes quite a while to perform, you might want to check to see if a recalculation is necessary before actually forcing one. It appears that there is no flag you can directly check to see if a recalculation is necessary. The closest thing is to check the Workbook object's Saved property. This property essentially acts as a "dirty flag" for the entire workbook. If there are unsaved changes in a workbook, then the Saved property is False; if everything is saved, then it is True.
How does this help you figure out if a recalculation is necessary? Remember that calculation is only necessary when there are changes in a worksheet. Changing anything in a worksheet will also set the workbook's Saved property to False. Thus, you could check the Saved property before doing the recalculation, as shown here:
If Not ActiveWorkbook.Saved Then ActiveSheet.Calculate End If
There is only one problem with this approach, of course—the Saved property is only set to True if the workbook is actually saved. This means that you could recalculate multiple times without really needing to do so, unless you tie saving and recalculation together, as shown here:
If Not ActiveWorkbook.Saved Then ActiveSheet.Calculate ActiveWorkbook.Save End If
The wisdom of approaching this problem in this manner depends on the nature of your particular situation. If it takes longer to save the workbook than it does to simply recalculate, then this approach won't work. If, however, recalculation takes longer (which is very possible with some types of operations), then this approach may work well.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5685) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Determining if Calculation is Necessary.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Want a quick way to speed up your macros? All you need to do is to stop Excel from updating the screen while the macro is ...
Discover MoreHaving macros in multiple open workbooks can sometimes produce unexpected or undesired results. If your macros are ...
Discover MoreExcel keeps track of the actions you take so that you can undo those actions if any are taken in error. You may want to ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2019-05-10 10:32:15
Dennis Costello
Willy is correct but ... it's hard to imagine a case where a recalc would be needed and the .Saved property is still True. So Allen's advice here is if nothing else a safe approach to the problem.
2018-12-22 11:19:13
Willy Vanhaelen
There are a lot of actions in Excel which will set the "dirty flag" to TRUE, as for instance change the formatting of a cell or change the margins in print setup... Running the macro now will cause the workbook to recalculate altough you didn't change anything that makes a recalculation necessary.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments