Home > Software design >  How to run Python script in VBA by reading the path that is in the cell?
How to run Python script in VBA by reading the path that is in the cell?

Time:03-12

I need help.

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

So I'm doing this to facilitate a way that people don't have to go into the VBA configuration to make the script path changes.

Really appreciate if you can help, Thanks

CodePudding user response:

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\hello.py"
    
    ' /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
  • Related