在Excel VBA中运行RScript时出错

时间:2016-04-14 21:47:43

标签: r excel vba shell excel-vba

我已经针对同样的问题搜索了几个解决方案,但我的代码仍然无效。确保我的文件路径中没有空格,并且仍然有三重引用以确定。我得到了"方法'运行'对象' 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

2 个答案:

答案 0 :(得分:4)

我认为您的报价水平不正确。 RScript应该是引号,而脚本的文件名不应该是。此外,我想确保在通话中包含完整路径名称。尝试:

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

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

答案 1 :(得分:0)

花了一点力气才能实现这一目标,但以下对我有用。

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