简体   繁体   中英

using VBA to send data to R

First of all, I am new to excel Visual Basic. I am attempting to write code that takes 3 columns of user-defined data to use in R (for graphing purposes). User will hit button in excel that creates graph output from R. I have been able to, at the very least, interact with R following this example: http://shashiasrblog.blogspot.com/2013/10/vba-front-end-for-r.html

I am having trouble extending this to include multiple cell inputs and form a data frame in R. Here is my attempt:

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

I am getting an error: "Compile error: Type mismatch". I'm assuming I'm defining Attributes() incorrectly. Any help or direction to literature/tutorials is greatly appreciated! In all honesty, I'm not understanding the path and errorCode lines.

Thanks all, J

This code did the trick to write to a csv file: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM