0

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.

  • Does this answer your question? [Return result from Python to Vba](https://stackoverflow.com/questions/39516875/return-result-from-python-to-vba) – Pᴇʜ May 05 '20 at 06:11
  • Thanks for the feedback. I am afraid that I have tried to implement this solution without success. I forgot to add it to my question. – Mose Mehari Gebreselassie May 05 '20 at 13:10
  • Please [edit] your question and show your attempt then. Because the solution I linked should work. We can only tell what was wrong with your attempt if you show it. • Also note that *"without success"* in no useful error description. So please also tell what went wrong when you tried this. – Pᴇʜ May 05 '20 at 13:13

0 Answers0