ExcelPython返回types不匹配问题

所以我使用VBA和ExcelPython引用从Excel文件中调用python脚本。 python脚本工作正常除了Excel不断告诉我,我有一个types不匹配的行:

Function calcCapValue(times As Range, fRates As Range, strike As Range, vol As Range, delta As Double, pv As Range) As Variant Set methods = PyModule("PyFunctions", AddPath:=ThisWorkbook.Path) Set result = PyCall(methods, "CalculateCapValue", KwArgs:=PyDict("times", times.Value2, "fwdRates", fRates.Value2, "strike", strike.Cells(1, 1).Value2, "flatVol", vol.Cells(1, 1).Value2, "delta", delta, "pv", pv.Cells(1, 1).Value2)) calcCapValue = PyVar(PyGetItem(result, "res")) ' <--- TYPE MISMATCH HERE Exit Function End Function 

无法弄清楚为什么,我从这里的示例代码: https ://sourceforge.net/p/excelpython/wiki/Putting%20it%20all%20together/和在这里: http://www.codeproject。 COM /条/ 639887 /调用的Python代码从- Excel的使用,ExcelPython

仍然得到这种types不匹配,我不明白为什么。

这是python脚本:

 #imports import numpy as np from scipy.stats import norm # # Cap Price Calculation # def CalculateCapValue(times, fwdRates, strike, flatVol, delta, pv): capPrice = 0; #Iterate through each time for caplet price for i in range(0, len(times)): ifr = float(fwdRates[i][0]) iti = float(times[i][0]) d1 = (np.log(ifr/strike)+((flatVol**2)*iti)/2)/(flatVol*np.sqrt(iti)) d2 = d1 - (flatVol*np.sqrt(iti)) Nd1 = norm.cdf(d1) Nd2 = norm.cdf(d2) capPrice += pv*delta*(ifr*Nd1 - strike*Nd2) return {"res": capPrice} 

谢谢!

在Sourceforge的ExcelPython论坛上回答问题:

http://sourceforge.net/p/excelpython/discussion/general/thread/97f6c1b0/