xlwings带有函数参数的RunPython

问题描述 投票:0回答:1

我试图从excel中的按钮运行RunPython函数。我希望函数从excel中的一个框中获取参数,如下所示:

Sub ImportData()
    Dim choice As String
    choice = Range("B2").Value
    RunPython ("import Excel_module; Excel_module.importing_data(choice)")
End Sub

这有可能吗?如果是这样,我该如何编写代码?我宁愿不使用UDF。

python vba xlwings
1个回答
0
投票

你可以用两种方式做到这一点:

1)通过使用RunPython

您需要使用字符串连接:

RunPython ("import Excel_module; Excel_module.importing_data(" & choice & ")")

2)或者,如果您只在Windows上,那么您也可以使用xw.sub装饰器并使用VBA中的导入函数,就像它是本机VBA函数一样,请参阅docs。它目前没有在文档中显示,但它们接受参数。

© www.soinside.com 2019 - 2024. All rights reserved.