简体   繁体   English

使用 Excel 输入工作簿中的 VBA 按钮运行 python 代码 (Anaconda3)

[英]Run a python code (Anaconda3) using a VBA button in the Excel input workbook

I wrote a Python code that performs some calculations using as input data in an excel workbook.我编写了一个 Python 代码,它使用 excel 工作簿中的输入数据执行一些计算。 My goal is to add a button inside the input excel workbook that automatically run the python script.我的目标是在输入 excel 工作簿中添加一个自动运行 python 脚本的按钮。 I have already added the button to which I've associated the following macro:我已经添加了与以下宏相关联的按钮:

''' '''

Sub RunPythonScript()

    Dim objShel As Object
    Dim PythonExe, PythonScript As String
    
    Set objShell = VBA.CreateObject("Wscript.Shell")
    
    PythonExe = "*path to python.exe*"
    
    PythonScript = """*path to the code*"""
    
    objShell.Run PythonExe & PythonScript

End Sub

''' '''

The macro isn't working and returns the error Method 'Run' of object IWshShell3 failed .宏不工作并返回错误Method 'Run' of object IWshShell3 failed I don't know if the issue is due to the fact that I have the input excel workbook for the python code open (usually should be closed to make the code works).我不知道这个问题是否是因为我打开了 python 代码的输入 excel 工作簿(通常应该关闭以使代码正常工作)。

maybe you can try put that run python command into a bat file then run that bat file也许您可以尝试将运行 python 命令放入 bat 文件中,然后运行该 bat 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM