8

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
  • I opened excel, imported the functions using import function in xlwings addin. I found no errors in importing the functions
  • I called the functions from the excel cell, "=exponent(A1,B1)"

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.

Vivek
  • 81
  • 1
  • 3
  • 3
    Did you follow the basic steps to enable Trust access and add a reference to xlwings in the VBA editor? see http://docs.xlwings.org/en/stable/udfs.html#one-time-excel-preparations and http://docs.xlwings.org/en/stable/addin.html#installation – Felix Zumstein Jul 15 '17 at 11:45
  • Thank You @FelixZumstein :) . I haven't added the reference in the VBA Editor before That was the problem. I added that as mentioned in the Docs, and It is working now. Thank you for the right on the spot answer. – Vivek Jul 15 '17 at 12:12
  • Thanks Felix Zumstein. – cmoman Mar 10 '18 at 02:30
  • 2
    As previously mentioned, you have to add a reference in VBA. Open Developer console (Alt-F11)-> Tools -> References -> select xlwings But there is a catch to it, in my case, it is deselecting the reference to defaults every time I open a new notebook, so just make sure it is checked every time. Had a hard time due to this thing. – Jay523 Mar 04 '20 at 02:20
  • I have the same issue, but I have the xlwings reference checked, but still get the same "Object Required" error. Any ideas? – otterdog2000 Sep 11 '20 at 23:00

1 Answers1

21

You need to add the Reference in VBA.
Open up the Developer console (Alt-F11) Click on Tools -> References and select xlwings

ajaali
  • 860
  • 12
  • 16