0

I have tried several solutions to run python script out of VBA.

My current solution that still doesnt work

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim windowStyle As Integer: windowStyle = 1
Dim waitOnReturn As Boolean: waitOnReturn = True

wsh.Run "C:\Python33\python.exe C:\Users\***\Documents\Promo models\get_sku_data.py", windowStyle, waitOnReturn

Any ideas?

6
  • This seems to be a VBA question, not a Python question. The trouble is with starting the Python interpreter process, not with the script itself. Also, why would you do this? ;) Commented Jun 21, 2016 at 14:50
  • I was asked to directly present my model results on Excel sheet for business people Commented Jun 21, 2016 at 14:55
  • Great, there are various Python libraries for modifying Excel sheets. No need to run Python via VBA.. Commented Jun 21, 2016 at 14:57
  • 1
    Yeah, of course, but the problem is that I have to make it as a Excel-button solution =) Commented Jun 21, 2016 at 15:06
  • "Run" paths with spaces should likely be quoted. Commented Jun 21, 2016 at 16:11

3 Answers 3

0

xlwings claims to make python from excel very easy:

Sub RandomNumbers()
    RunPython ("import mymodule; mymodule.rand_numbers()")
End Sub

To make this run, just import the VBA module xlwings.bas in the VBA editor (Open the VBA editor with Alt-F11, then go to File > Import File... and import the xlwings.bas file. ). It can be found in the directory of your xlwings installation.

Sign up to request clarification or add additional context in comments.

4 Comments

I would like not to use VBA extensions cause I need to call python script on other computers which will require Excel changes many times. Any way to call python script with default libs?
So excel runs on computer A but you need to call a python script on Computer B? For excel without xlwings I'm out of my knowledge.
No, it will work on different but same computers. On computer A both script and Excel will work together, the same config on computer B and etc. I just dont want to install this lib everywhere if possible
Ah okay. Maybe wsad597 answer can help you then.
0

Finally I ended up with creation of .exe files instead of trying to call scripts itself.

So, I applied cx_Freeze, created .exe files and then called them as

Script_path1 = "C:\Users\" + Environ("Username") & "\Documents\Promo_models\get_sku_data\gui_changed.exe"
wsh.Run Script_path1, windowStyle, waitOnReturn

Comments

0

Here are a couple options to try.

RetVal = Shell("<full path to python.exe> " & "<full path to your python script>")

Or if the python script is in the same folder as the workbook, then you can try :

RetVal = Shell("<full path to python.exe> " & "ActiveWorkBook.Path & \<python script name>")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.