1

I call a python script from VBA to do a fit that excel cannot do and scipy has a convenient function for it. So I use this code to call the function and get and set data in VBA:

Public Function GetData()
  GetData = Cache
  Cache = Empty
End Function

Public Sub SetData(data)
  Dim ok As Boolean
  Cache = data
End Sub

Sub LSQCBSplineIO(targetrng As Range)
Dim oShell As Object
Dim OutputArr() As Double
Dim InputArr() As Double
Dim ok As Boolean

Call getinputarray(targetrng, InputArr) ' This works, tested it
Set oShell = VBA.CreateObject("WScript.Shell")

' Make the data available via GetData()'
Cache = InputArr

Debug.Assert 0 = oShell.Run("<FullpathtoScript>.py", 0, True)

' Handle the returned data '
targetrng = Cache

End Sub

This does work according to the answer to this question.

The python code I call is the following:

import win32com.client
import numpy as np
import os
import matplotlib.pyplot as plt
import win32com.client
from scipy.interpolate import LSQUnivariateSpline, UnivariateSpline

print "Hello, User!"

# get the running instance of Excel
app = win32com.client.GetObject(Class="Excel.Application")

# get some data from Excel
data = app.Run("GetData")
ssc=np.array(data)

# do fitting ...
<insert fitting code here>

# return some data to excel
app.run("SetData")

The program is basically this:

  • VBA calls Python

  • Python calls VBA and gets data

  • Python does stuff

  • Python calls VBA and sets data

  • Python is done and VBA takes over again

If now Python has an error VBA will get stuck. VBA doesn't get an errormessage and hangs in there forever, causing excel to crash. So a bug in python means that I need to forcequit excel afterwards. The python script above is made to be called from VBA because only then the required data is at the right place for it to be gathered.

The problem is now: That this cannot be debugged in python and not in VBA.

In python the program doesn't work because it is not meant to work like that and in VBA I get no errormessage and the program gets stuck.

So how does one debug something like this?

OR even better

Is there a smarter way to move data between python and VBA?

Footnote: I initially used .txt files to intermediately, so python would have a input.txt and an output.txt. This was slow though and had issues running at full speed. See my other question.

13
  • What can't VBA do? Commented Aug 22, 2018 at 8:39
  • @Tom When calling python script above the VBA program above does not print the output from the python script. This way one is completely blind of what is going wrong on the python side. Is that a better explaination? Commented Aug 22, 2018 at 8:42
  • 2
    ah fair enough. Have you thought about using something like xlWings so that it can all be done in Excel instead of passing it to an eternal application? Commented Aug 22, 2018 at 8:57
  • 1
    @Dan I implemented the second solution from this post: stackoverflow.com/questions/45105388/… This works like a charm and prints the errormessages into a VBA errormessage. This solves the issue then. Commented Aug 22, 2018 at 13:39
  • 2
    Possible duplicate of Moving numpy arrays from VBA to Python and back Commented Aug 22, 2018 at 13:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.