Skip to content

Latest commit

 

History

History
49 lines (29 loc) · 2.02 KB

Addin01.md

File metadata and controls

49 lines (29 loc) · 2.02 KB

Loading the ExcelPython add-in

  • Download the latest release and unzip it somewhere.

  • Open the add-in xlpython.xlam in Excel.

  • If all goes well you should see the ExcelPython tab in Excel's toolbar.

    image

  • You may get an error saying Programmatic access to Visual Basic Project is not trusted. If so check out the add-in troubleshooting guide.

Note that it is possible to permanently install the add-in so you don't need to open it manually each time.

Writing a user-defined function in Python

To interact with Python, a workbook must first be setup to use ExcelPython. To do this it is first necessary to save it as a macro-enabled workbook.

  • Choose an empty folder and in it save an empty workbook as Book1.xlsm.

  • From the ExcelPython tab in the toolbar click 'Setup ExcelPython'.

Next write your user-defined function in Python. In the previous step ExcelPython will have created a file called Book1.py in the same folder as Book1.xlsm in which the Python functions to be used in the workbook can be defined.

  • Edit Book1.py to contain the following code:

    # Book1.py
    from xlpython import *
    
    @xlfunc
    def DoubleSum(x, y):
    	'''Returns twice the sum of the two arguments'''
    	return 2 * (x + y)
  • Switch back to Excel and click 'Import Python UDFs' in the ExcelPython tab to pick up the changes made to Book1.py.

  • Enter the formula =DoubleSum(1, 2) into a cell and you should get the correct result:

    image

  • Note that the DoubleSum function is usable from VBA as well. Open the VBA window (Alt+F11), switch to the Immediate Window (Ctrl+G) and type

    ?DoubleSum(1, 2)
    

To continue move onto the next tutorial.