[Solved] Run R function in VBA macro


You can run an R script in VBA by creating Windows Shell obejct and passing it a string that executes an R script

Sub RunRscript()
'runs an external R code through Shell
'The location of the RScript is 'C:\R_code'
'The script name is 'hello.R'

Dim shell As Object
Set shell = VBA.CreateObject("WScript.Shell")
Dim waitTillComplete As Boolean: waitTillComplete = True
Dim style As Integer: style = 1
Dim errorCode As Integer
Dim path As String
path = "RScript C:\R_code\hello.R"
errorCode = shell.Run(path, style, waitTillComplete)
End Sub

(source)

You can pass in your cell values as command line args to the R script. See ?commandArgs for more.

2

solved Run R function in VBA macro