使用VBA向R发送数据

时间:2016-09-06 22:14:19

标签: r excel vba excel-vba

首先,我是excel Visual Basic的新手。我试图编写代码,在R中使用3列用户定义的数据(用于图形目的)。用户将点击excel中的按钮,从R创建图形输出。我已经能够在此示例之后与R进行交互:http://shashiasrblog.blogspot.com/2013/10/vba-front-end-for-r.html

我无法将其扩展为包含多个单元格输入并在R中形成数据框。这是我的尝试:

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 Attributes() As Variant
Attributes = Sheet55.Range("A2:C68")

Dim path As String
path = "RScript S:\...\test.R " & Attributes
errorCode = shell.Run(path, style, waitTillComplete)

End Sub

我收到错误:"编译错误:类型不匹配"。我假设我没有错误地定义属性()。非常感谢任何文学/教程的帮助或指导!说实话,我并不了解路径和errorCode行。

谢谢大家, Ĵ

1 个答案:

答案 0 :(得分:0)

此代码可以写入csv文件:http://www.excel-easy.com/vba/examples/write-data-to-text-file.html

Private Sub CommandButton21_Click()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer

myFile = "...\test.csv"
Set rng = Selection

Open myFile For Output As #1

For i = 1 To rng.Rows.Count
  For j = 1 To rng.Columns.Count
        cellValue = rng.Cells(i, j).Value

        If j = rng.Columns.Count Then
            Write #1, cellValue
        Else
            Write #1, cellValue,
        End If
    Next j
Next i

Close #1


End Sub