How do I send/read data from VBA in Python?

Question:

Background

Right now I’m creating a macro to help automate the creation of some graphs in VBA. However, the creation of the graphs requires specific tasks to be done, for example, certain points in a series to be larger depending on previous instances. I would much rather do this data manipulation in python.

Problem

I want to use excel for its user-friendly interface but want to handle all the data manipulation within Python. How can I send data I create in VBA to python. To clarify I’m not trying to read specific cells in the excel sheet.

If I define a string in VBA say…

 Dim example_string as String

 example_string = "Hello, 1, 2, 3, Bye"

How can I send this information I created within VBA to Python for manipulation?

More Specifics

I have a textbox in excel that is filled by the user, which I read using VBA. I want to send that txt data from VBA to python. The user highlights the desired cells, which are not necessarily the same each time, clicks a button and fills a textbox. I don’t want to use range or specific cell selection since this would require the user to specifically enter all the desired data into cells (too time-consuming).

I want to understand the basic procedure of how to send data between VBA and python.

Asked By: Markus T.

||

Answers:

You can do the whole thing in python, it will be more efficient and you can either use excel or sqlite3 as database, go here to read about graphic interfaces with tkinter, use pandas and numpy to process your data.

If you insist in sending data to python, import sys to your python script to read parameters and then run it from vba with the shell() method.

EDIT: You wanted an example, here it is =>

Open a new excel file, create a procedure like this (VBA CODE):

Sub sendToPython()

    Dim shell As Object
    Dim python As String
    Dim callThis As String
    Dim passing

    Set shell = VBA.CreateObject("Wscript.Shell")

    '/* This is where you installed python (Notice the triple quotes and use your own path *always)*/
    python = """C:UsersyourUserNameappdatalocalprogramspythonpython37python.exe"""

    '/* This is the data you'll be passing to python script*/
    passing = "The*eye*of*the*tiger" 

    callThis = "C:UsersyourUserNamedesktopyourScriptName.py " & passing & ""

    shell.Run python & callThis 

End Sub

The idea is to create some kind of a parser in python, this is my silly example (PYTHON CODE):

import sys

f = open("log.txt", "w")
arg = (sys.argv[1]).split("*")
s = " "
arg = s.join(arg)
print("This is the parameter i've entered: " + arg, file=f)

Notice how i used sys to read a parameter and i exported to actually see some results because otherwise you’ll just see a black screen popping up for like a millisecond.

I also found this article, but it requires you to wrap the python script in a class and i don’t know if that works for you

Answered By: Marlond25

i thing if i create a text file in anyway in start excel by vba code and send your data to text file and read this when the python code start run and python open text file and read data
this is a way that you can send some data to python script from excel that may open in any were

Answered By: Milad Ghalehnoei
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.