How to run Python script in VBA by reading the path in a cell?


I need to close a project, but most people cannot configure VBA to change paths (path made the change).

Sub RunPython_Click()

Dim objShell As Object
Dim PythonExe, PythonScript As String
    Set objShell = VBA.CreateObject("Wscript.Shell")
    PythonExe = Range("B13").Value
    PythonScript = Range("B14").Value
    objShell.Run PythonExe & " " & PythonScript
End Sub

enter image description here

I’m doing this so people don’t have to go into the VBA configuration to make the script path changes.


You can try this instead:

Sub RunPython_Click()
    Dim PythonExe, PythonScript As String
    'hard-coded for testing...
    PythonExe = "C:\Python\Python39-32\Python.exe"
    PythonScript = "C:\Python\Code\"
    ' /k keeps the window open so you can see any problems
    ' swap /k to /c when sure there's no errors
    Shell "cmd.exe /k """"" & PythonExe & """ """ & PythonScript & """""", vbNormalFocus
End Sub

Answered By – Tim Williams

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published