Python in Excel: Controlling Python Calculation

Because Python calculations can be slow it is worth looking at ways of controlling Python calculation.

Partial Calculation

The calculation setting that used to be called “Automatic except Data Tables” has been renamed to “Partial”. Partial Calculation works like Automatic Calculation mode except that Data Tables and Python cells are NOT automatically calculated. Pressing F9 or the Calculate Now button will trigger recalculation of any Python cells and Data Tables that need to be calculated.

Python cells within a workbook are still treated as Co-Volatile – if one PY cell calculates then all the PY cells in the workbook recalculate.

Disabling/Enabling the Python Calculations on a Worksheet

If you have some PY cells on a worksheet that you do not want to calculate (perhaps because they are very slow) but you do want to enable PY calculation on other worksheets, you can use the Worksheet.EnableCalculation property. This could give you some control over Python Co-Volatility.

You can change this property using the Developer Tab and Properties, or by sheet properties from the VBE (select the worksheet in the VBE and press F4), or by using a VBA macro.

When this property is False none of the Excel formulas or Python cells on that worksheet can be calculated.
This also means that any Python variable definitions or other actions from this sheet that affect the Global Python space will not occur.

Toggling EnableCalculation from False to True dirties all the cells on the worksheet and triggers a recalculation.

The EnableCalculation property is not preserved in a saved workbook.

Conclusion

Partial calculation mode is a useful way of controlling slow Python calculations.

Finer control can be achieved by using the Worksheet.EnableCalculation property to isolate particular worksheets within a workbook.

This entry was posted in Calculation, Excel, Python, VBA and tagged , , , . Bookmark the permalink.

Leave a comment