Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

user defined functions - Error when running a xlwings UDF in VBA but not directly in excel

I have build a function with different arguments in Python which I can recall in xls using xlwings. When I try to use the same function in VBA though I get the following error:

AttributeError:'int' object has no attribute 'worksheet' self.xl.Worksheet

This is the code in vba

Sub RefreshPrices()

Dim result As Variant
Dim wb As Workbook: Set wb = ActiveWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets("Power Curve")

For i = 0 To 7
result = getPRICES_FWD(Cells(2, 1), Cells(6, 3 + i), Cells(7, 1), Cells(8, 2), Cells(9, 2))
wb.ws.Range(Cells(7, 3 + i * 4).Address, Cells(16, 4 + i * 4)).Value = result

Next

End Sub


'Autogenerated code by xlwings - changes will be lost with next import!
#Const App = "Microsoft Excel" 'Adjust when using outside of Excel
Function getPRICES_FWD(as_of_date, curveName, granularity, start_date, end_date)
    If (Not Application.CommandBars("Standard").Controls(1).Enabled) Then Exit Function
    #If App = "Microsoft Excel" Then
        If TypeOf Application.Caller Is Range Then On Error GoTo failed
        getPRICES_FWD = Py.CallUDF("Prices", "getPRICES_FWD", Array(as_of_date, curveName, granularity, start_date, end_date), ThisWorkbook, Application.Caller)
        Exit Function
    #Else
        getPRICES_FWD = Py.CallUDF("Prices", "getPRICES_FWD", Array(as_of_date, curveName, granularity, start_date, end_date))
        Exit Function
    #End If
failed:
    getPRICES_FWD = Err.Description
End Function

Any help would be much appreciated

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...