在Excel VBA中运行RScript时出错

我已经通过几个解决scheme来解决这个问题,但是我的代码仍然无法工作。 确保在我的文件path中没有空格,并且仍然有三重引号可以确保。 我得到运行macros的对象'IWshShell3'失败'的错误'方法运行'。 我可以在这里错过什么?

码:

Sub RunRscript() 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 ""G:\structureshouston\Kocian\hello.R""" errorCode = shell.Run(path, style, waitTillComplete) End Sub 

我相信你的报价水平是不正确的。 RScript应该在引号中,而脚本的文件名不应该是。 另外,我想确保在呼叫中包含完整的path名称。 尝试:

 path = """C:\Program Files\R\R-3.2.4revised\bin\RScript"" G:\structureshouston\Kocian\hello.R" 

您可能需要根据您安装的版本更新RScript的path。

花了一点点努力去实现这一目标,但以下工作为我。

 Sub RunRscript1() 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 to R executable: C:\Users\rshuell001\Documents\R\R-3.2.5\bin\x64\R.exe ' path to R script: C:\Users\rshuell001\Documents\R\Download.r ' see more setup details here ' http://shashiasrblog.blogspot.com/2013/10/vba-front-end-for-r.html path = "C:\Users\rshuell001\Documents\R\R-3.2.5\bin\x64\R.exe CMD BATCH --vanilla --slave C:\Users\rshuell001\Documents\R\Download.r" 'path = """C:\Users\rshuell001\Documents\R\R-3.2.5\bin\i386"" C:\Users\rshuell001\Documents\R\Download.R" errorCode = shell.Run(path, style, waitTillComplete) End Sub 

请确保您阅读使用下面的链接设置“权限”。

http://shashiasrblog.blogspot.com/2013/10/vba-front-end-for-r.html