A Better Excel Goal Seek using Python
By: Tony Roberts
Tags: Pyxll, Excel, Ms Office, Scipy, Python

Solving has been artifically slowed down for visualization.

Microsoft Excel has a great feature, “Goal Seek”, under the Data / What If… menu. Given a formula, it will calculate what the input needs to be in order for that formula to equal a specific result. It’s a very powerful but simple tool.

The standard “Goal Seek” feature while useful in many situations is somewhat limited. It can only solve for a single input cell, and there are no controls over the algorithm used.

There is the more sophisticated “Solver” add-in that also comes as part of Excel. This is like “Goal Seek” on steroids! It can handle multivariate functions, constraints, and has multiple optimization algorithms to choose from.

What if this still isn’t enough though? For more complex problems like global optimization or large scale optimizations, the default algorithms in the “Solver” add-in may not be sufficient.

Python and scipy.optimize

Python is many data scientists go-to tool, and for good reason! For optimization problems that go beyond the simple “Goal Seek” or “Solver” solutions found in Excel, the Python package scipy.optimize offers a more comprehensive array of algorithms.

Using scipy.optimize is a great solution if your model can easily be re-written in Python. However, if your model is already in Excel, or you prefer to stay in Excel, it is still possible to leverage the scipy.optimize functions from within Excel.

If you are new to Python and want to learn you can download the free e-book “The Ultimate Python Guide for VBA Developers™” here:

https://www.pyxll.com/ultimate-python-guide-for-vba-developers.html

Python Excel Integration With PyXLL

If you’re an Excel user and you want to use Python as a back-end to your workbooks, or if you’re a Python user and you want to use Excel as a front-end for your Python code, you can use PyXLL, the Python Excel Add-In.

There is no need to choose between the interactivity of Excel and the power and productivity of Python as you can integrate Python into Excel, and even use Python as a complete replacement for VBA.

PyXLL allows you to write worksheet functions (UDFs), Excel Macros, menus functions, ribbon toolbars, RTD functions and more all using nothing but Python — no VBA required.

Calling scipy.optimize from Excel

With Python running in Excel, we can now use scipy.optimize directly from within Excel.

To do the equivalent of Excel’s Goal Seek in Python, we would do something like:

from scipy.optimize import minimize minimize(objective_func, X)

Where X is the initial value of the inputs to the objective function. The objective function is what we’re trying to minimize, which in our case is an Excel formula.

Our objective function needs to wrap our model in Excel. In the same way as when we use Goal Seek, our model is simply a set of input cells and an output cell. Our objective function will set the values of the input cells, calculate the worksheet, and then read back the value of the output cell.

A simple case could be where the inputs were in cells C2 and C3, and the output was in C4. PyXLL makes it simple to access the Excel Application object and access cell values in the same way as you would in VBA:

from pyxll import xl_app

def objective_func(arg): xl = xl_app() # Copy argument values to input range xl.Range('C2').Value = float(arg[0]) xl.Range('C3').Value = float(arg[1]) # Calculate after changing the inputs xl.Calculate() # Return the value of the output cell result = xl.Range("C4").Value return result

Now we’ve got an objective function, we need some way of calling the optimizer. We can do this using an Excel macro written in Python using PyXLL’s @xl_macro decorator.

from pyxll import xl_macro, xl_app @xl_macro(shortcut='Ctrl+Alt+P') def optimize():

# Get the initial values of the input cells xl = xl_app() x = xl.Range("C2").Value y = xl.Range("C3").Value X = np.array([x, y]) # run the minimization routine minimize(obj_func, X, method='nelder-mead')

This macro can be assigned to a button or called via the shortcut specified.

Further Reading

There are more technical details and in-depth explanations of using Python’s scipy.optimize from Excel with PyXLL in this blog post:

https://www.pyxll.com/blog/a-better-goal-seek/

For more details about PyXLL, please see

https://www.pyxll.com.

If you are new to Python and want to learn download the free e-book “The Ultimate Python Guide for VBA Developers™” here:

https://www.pyxll.com/ultimate-python-guide-for-vba-developers.html

Please also see the scipy.optimize documentation here

https://docs.scipy.org/doc/scipy/reference/optimize.html