Python 将值从xlwings返回到vba
让我们使用上的示例 给定以下python代码:Python 将值从xlwings返回到vba,python,vba,excel,xlwings,Python,Vba,Excel,Xlwings,让我们使用上的示例 给定以下python代码: import numpy as np from xlwings import Workbook, Range def rand_numbers(): """ produces std. normally distributed random numbers with shape (n,n)""" wb = Workbook.caller() # Creates a reference to the calling Excel f
import numpy as np
from xlwings import Workbook, Range
def rand_numbers():
""" produces std. normally distributed random numbers with shape (n,n)"""
wb = Workbook.caller() # Creates a reference to the calling Excel file
n = int(Range('Sheet1', 'B1').value) # Write desired dimensions into Cell B1
rand_num = np.random.randn(n, n)
Range('Sheet1', 'C3').value = rand_num
这是最初的例子
假设我们将其稍微修改为:
import numpy as np
from xlwings import Workbook, Range
def rand_numbers():
""" produces std. normally distributed random numbers with shape (n,n)"""
wb = Workbook.caller() # Creates a reference to the calling Excel file
n = int(Range('Sheet1', 'B1').value) # Write desired dimensions into Cell B1
rand_num = np.random.randn(n, n)
return rand_num #modified line
我们使用以下调用从VBA调用它:
Sub MyMacro()
dim z 'new line'
z = RunPython ("import mymodule; mymodule.rand_numbers()")
End Sub
我们得到z
作为空值
是否有任何方法可以直接将值返回到vba,而无需写入文本文件或将值放在excel文档的第一位
感谢您的指点。根据xlwings文档,RunPython不允许您返回值。
要解决这些问题,请使用UDF,请参见VBA:用户定义函数(UDF)-但是,这目前仅限于Windows
我不使用python,但看看这是否是您想要的?谢谢,但问题是具体的。我感谢您的意见,但不幸的是,这不是我问题的目的(同样,这是我的错,不是你的错-由于其他基础设施的原因,范围只需要与xlwings对话)。