Is there a way to call a Python code in Excel-VBA? Is there a way to call a Python code in Excel-VBA? vba vba

Is there a way to call a Python code in Excel-VBA?


The simplest way is to run the python interpreter with the Shell command

Shell ("python.exe " & yourScript & " " & arguments)


Yes, there is. My preferred way of doing this is through xlwings (https://www.xlwings.org/), but there are several other options as well. XlWings is great because it's free, open source and easy to use, with great documentation. There are some feature limitations though, so you'd have to check if it fits your needs.


There are multiple ways tu run a python script with VBA depending on whether you need to wait for the end of the execution and know if it went without error.

With Shell, asynchronous with console:

Public Sub RunPython(file As String, ParamArray args())  Shell "python.exe """ & file & """ " & Join(args, " ")End Sub

With Shell, synchronous without console:

Public Function RunPython(file As String, ParamArray args())  Shell "pythonw.exe """ & file & """ " & Join(args, " ")End Function

With WScript.Shell, synchronous without console and with exit code:

Public Function RunPython(file As String, ParamArray args()) As Long  Dim obj As Object  Set obj = CreateObject("WScript.Shell")  RunPython = obj.Run("pythonw.exe """ & file & """ " & Join(args, " "), 0, True)End Function