title: Passing Variables from VBA to R-Script
body: <p>I am currently working on an Interface which connects R and Excel. I am up to now able  to execute a R programm via a VBA Macro. 
  I used the recommandations given on
  <a href="http://shashiasrblog.blogspot.fr/2013/10/vba-front-end-for-r.html" rel="nofollow">http://shashiasrblog.blogspot.fr/2013/10/vba-front-end-for-r.html</a></p>
  
  <p>Since my R-Script invokes external txt-files, it would be nice not to change the R code manually, when changing the directories of the corresponding files. 
  So the question is, <strong>is it possible to assign variables from a VBA Script to R</strong>.</p>
  
  <p>I did not manage to be successful with the help of the indicated website. Did anybody meet this problem and knows how to resolve it? You will find attached a code example which I tried to adapt from the website.</p>
  
  <p>Greetings,</p>
  
  <p>David</p>
  
  <pre><code> 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 var1, var2 As Double
  var1 = Tabelle1.Range("D5").Value
  var2 = Tabelle1.Range("F5").Value
  
  Dim path As String
  path = "RScript C:\Users\David\Desktop\Test\test.R" &amp; var1 &amp; " " &amp; var2
  
  errorCode = shell.Run(path, style, waitTillComplete)
  
  End Sub
  
  
  
  args&lt;-commandArgs(trailingOnly=T)
  # cat(paste(args,collapse="\n"))
  sink('C:/Users/David/Desktop/Test/test.R',append=F,type="output")
  
  var1&lt;-as.numeric(args[1])
  var2&lt;-as.numeric(args[2])
  
  var1&lt;-5
  var2&lt;-2
  a&lt;-var1+var2
  write.table(a, file = "C:/Users/David/Desktop/bla.txt", sep = ",", col.names = NA,
              qmethod = "double")
  sink(NULL)
  </code></pre>