I'm a newbie into both python and xlwings. So the story is,
I need a custom function needs to be used in Excel. Since I have no clue about VB scripts, I decided to write a function in python and import it in excel using xlwings.
I installed xlwings using the pip command. I added the addin to excel by the procedure given in xlwings support forum.
I created an excel file, say " Test.xlsm". I created a python file in the same name "Test.py" (File is in same folder only)
I wrote my function in the python
import xlwings as xl
@xl.func
def exponent(x,y):
#the function is an example only. I tried this for practicing and it is also not working
z=x**y
return z
Instead of getting a result, I'm getting "Object Required" I don't know what went wrong?
Any ideas what I'm missing? Forgive me for the basic question.
Call Python with “RunPython”In the VBA Editor ( Alt-F11 ), write the code below into a VBA module. xlwings quickstart automatically adds a new module with a sample call. If you rather want to start from scratch, you can add a new module via Insert > Module .
Call Python from Excel To make this run, just import the VBA module xlwings. bas in the VBA editor (Open the VBA editor with Alt-F11, then go to File > Import File... and import the xlwings. bas file. ). It can be found in the directory of your xlwings installation.
Xlwings can be used to insert data in an Excel file similarly that it reads from an Excel file. Data can be provided as a list or a single input to a certain cell or a selection of cells.
27.0 and v0. 27.1 (Mar 8, 2022) Feature PRO This release adds support for xlwings Server to the Excel Desktop apps on both Windows and macOS. The new VBA function RunRemotePython is equivalent to runPython in the JavaScript modules of Google Sheets and Excel on the web, see xlwings Server (GH1841).
You need to add the Reference in VBA.
Open up the Developer console (Alt-F11)
Click on Tools -> References and select xlwings
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With