Assume we have a Julia script 'test.jl' in the same folder of an excel file. Is it possible to call this Julia script from Excel by using VBA? If yes, what the VBA code should it be? If no, any other methods? Thanks.
-
1Perhaps use Shell() to launch a process. Never heard of Julia. Get the Julia script to write its result to the file. In Excel check the process has completed and when so read the results file. Look here for example http://stackoverflow.com/questions/8902022/wait-for-shell-to-finish-then-format-cells – S Meaden May 31 '16 at 20:57
-
1I've got good experience using [BERT](https://bert-toolkit.com/) to call R from both Excel formulas and from VBA. It also allows Excel to be automated from R code. Although I've yet to try it, BERT now enables similar two-way communication between Julia and Excel. See [https://bert-toolkit.com/using-julia-with-bert](https://bert-toolkit.com/using-julia-with-bert) – Philip Swannell Aug 08 '18 at 08:31
1 Answers
You can certainly call any interpreting language script from VBA using a command line call with Shell()
. While not familiar with Julia, below are examples of VBA calling Python, PHP, and R scripts.
Shell "C:\Path\To\Python.exe C:\Path\To\PythonScript.py", vbNormalFocus
Shell "C:\Path\To\PHP.exe C:\Path\To\PHPScript.php", vbNormalFocus
Shell "C:\Path\To\RScript.exe C:\Path\To\RScript.R", vbNormalFocus
Plus, should you have above executable paths saved as environmental path variables, you can shorten the line:
Shell "python C:\Path\To\PythonScript.py", vbNormalFocus
Shell "php C:\Path\To\PHPScript.php", vbNormalFocus
Shell "RScript C:\Path\To\RScript.R", vbNormalFocus
If my guess is correct, simply locate the Julia executable and specific script, making sure to add quotes if spaces are in either paths as shown below:
Shell "C:\Path\To\Julia.exe C:\Path\To\Julia\Script.jl", vbNormalFocus
Shell "Julia ""C:\Path To\Julia\Script.jl""", vbNormalFocus
For a more child thread processing where you have more control, return codes, and even ability to pass arguments use VBA's Shell
object. Below passes two variable arguments:
Dim shell As Object
Dim waitTillComplete As Boolean: waitTillComplete = True
Dim style As Integer: style = 1
Dim path As String
Dim errorCode As Integer
Dim var1, var2 As Double
var1 = Range("A1").Value
var2 = Range("A2").Value
Set shell = VBA.CreateObject("WScript.Shell")
' BUILD COMMAND LINE STRING W/ WORKBOOK LOCATION AND ARGS
path = "Julia " & ActiveWorkbook.Path & "\JuliaScript.jl" & " " & var1 & " " & var2
' RUN COMMAND LINE
retVal = shell.Run(path, style, waitTillComplete)
Set shell = Nothing
Should you ever need to troubleshoot/debug, always know whatever string command line you build it should work in Command Prompt, PowerShell, or any other command line interpreters.
-
Thanks. I am running a Python script first to test but it says path not found. I am using mac, is there any difference? – ilovecp3 Jun 01 '16 at 17:42
-
Unix systems like Mac and Linux use forward slashes without drive letters. Did you use entire path of both executable and script: `"/path/to/python /path/to/python.py"`? Again, try running same string in terminal or Bash. And don't use *.exe* (if I remember). – Parfait Jun 01 '16 at 19:04
-