I have been trying to call a Python script from vba, but it is returning a number series instead of the desired output.
Here is the python script:
def hello():
df = pd.DataFrame(data=[2,2])
df.to_excel("hello_output.xlsx")
return "Hello"
hello()
I have tried to implement the vba scripts from the following stackoverflow posts and YT videos:
How to call python script on excel vba?
Run and execute a python script from VBA
https://www.youtube.com/watch?v=rlHcrAb2_fs&t=1s
https://www.youtube.com/watch?v=Z4SC53VZh-w
Here is my VBA script(I have tried with both one and two bakcslashes):
Sub RunPythonScript()
PythonExe = """C:\\Users\\username\\anaconda3\\python.exe"""
PythonScript = """C:\\Users\\username\\all scripts\\hello.py"""
retval = Shell(PythonExe & " " & PythonScript)
Worksheets("Sheet1").Range("D15").Value = retval
MsgBox (retval)
End Sub()
The file "hello_output.xlsx" is not created.
The MsgBox outputs a number series(probably some kind of process ID) instead of the string "Hello".
For your information I do have two python interpreters installed, and I have tried providing the file path to both of them. The anaconda python interpreter has the following error message when opening it manually:
"This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation"
I have written my python code in Spyder.
Any suggestions on how to fix this would be highly appreciated.