Run Python with VBA

Being able to read this article

  1. You will be able to start ** Command Prompt ** with ** Excel VBA **
  2. You will be able to execute ** Python ** from ** Excel VBA **

Launch a command prompt in Excel VBA

Start the command prompt with the code below and You can run Python.

    Dim suji1    As String
    Dim suji2    As String
    
    Dim WSH
    Dim wExec
    Dim cmd_str    As String
    
    suji1 = Range("D4").Value ''Get numbers from cells
    suji2 = Range("D5").Value
    
    Set WSH = CreateObject("WScript.Shell")

    py_file = ThisWorkbook.Path & "\Python.py"
    cmd_str = "python " & py_file & " " & suji1 & " " & suji2
    cmd_str = Replace(cmd_str, "\", "/")
    Set wExec = WSH.Exec("%ComSpec% /c " & cmd_str)
    
    Do While wExec.Status = 0
        DoEvents
    Loop
    
    Range("D6").Value = Val(wExec.StdOut.ReadAll) ''Receive results from Python

    Set wExec = Nothing
    Set WSH = Nothing

Commentary

    ''Object creation
    Set WSH = CreateObject("WScript.Shell")
    ''Python file path to run
    py_file = ThisWorkbook.Path & "\Python.py"
    ''Command creation
    cmd_str = "python " & py_file & " " & suji1 & " " & suji2
    cmd_str = Replace(cmd_str, "\", "/")
    ''Command execution
    Set wExec = WSH.Exec("%ComSpec% /c " & cmd_str)
    

With this, you can easily start the command prompt and You can start Python.

    ''Command creation
    cmd_str = "python " & py_file & " " & suji1 & " " & suji2

suji1 and suji2 are arguments. So you can get the value from the cell etc. and pass it.

Actually move

If you enter any value in number 1 and number 2, it will be calculated and output to the answer. スクリーンショット (3).png

By the way, Python is such a code.

import sys

def sum(suji1, suji2):
    return suji1 + suji2

if __name__ == "__main__":
    argv = sys.argv
    suji1 = str(argv[1])
    suji2 = str(argv[2])

    total = sum(suji1, suji2)

    print(total)

When executed, it looks like this. スクリーンショット (5).png

Finally

This time I started Python with VBA, but since I am actually running it using the command prompt I feel that I can do more if I apply it. Also, I will update it if I learn.

Recommended Posts

Run Python with VBA
Run prepDE.py with python3
Run Blender with python
Run iperf with python
Run python with PyCharm (Windows)
Run Python with CloudFlash (arm926ej-s)
Let's run Excel with Python
Run Label with tkinter [Python]
Run DHT22 with RasPi + Python
Run Rotrics DexArm with python API
Run mruby with Python or Blender
Run XGBoost with Cloud Dataflow (Python)
Run Aprili from Python with Orange
Run python3 Django1.9 with mod_wsgi (deploy)
Until you run python with apache
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Run servo with Python on ESP32 (Windows)
Uncle SES modernizes VBA app with Python
Run a Python web application with Docker
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python