I have the following Python script named 'func.py':
import numpy as np
def func(x, y):
return np.array([[x+y, x], [x-y, y]])
and I would like to use the function func
with a macro in Excel, I wrote this code for the macro:
Sub CallPythonFunction()
Dim x As Double
Dim y As Double
Dim wb As Workbook
Dim result As Variant
' Get the input values from Excel cells
x = Range("A1").Value
y = Range("B1").Value
' Call the Python function using xlwings
Set wb = ThisWorkbook
result = RunPython("import func; func.func(" & x & ", " & y & ")")
' Write the result to Excel
Range("C1").Value = result(1, 1)
Range("C2").Resize(result.Rows.Count, result.Columns.Count).Value = result.Value
End Sub
However, it does not work. When the cells A1 and B1 in excel are filled with the numbers 1 and 3 respectively, I got an error message 'error 13, type mismatch'.