Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run VBA script from R

Tags:

I have to manage a workflow involving R-scripts and VBA-code. I would like to run the process in R (where most of my code is) and now and then to call VBA-code for specific calculation.

I would prepare the inputs for VBA in R, write somewhere the results (.csv, database) and then use the results in the rest of the R-script.

The best would be of course to move the whole code into R but this is for now not possible. The VBA-code is fairly complex. Translating this into R will be a challenging long-term task.

Is there any possibility to manage in R such a work-flow?

like image 630
ddg Avatar asked Oct 16 '13 13:10

ddg


People also ask

How do I run a VBA file?

To run VBA in the “Microsoft Visual Basic for Applications” window, you can just press “F5” key button or click the “Run” icon in the toolbar.

How do I run VBA from the command line?

if you have the project file abc. rpy, copy your macro . vba file as abc. vba then use the runvbmacro switch to run the macro.


1 Answers

Here's a method which doesn't require a VBscript wrapper. You'll need to install the RDCOMClient package

library(RDCOMClient)  # Open a specific workbook in Excel: xlApp <- COMCreate("Excel.Application") xlWbk <- xlApp$Workbooks()$Open("C:\\Temp\\macro_template.xlsm")  # this line of code might be necessary if you want to see your spreadsheet: xlApp[['Visible']] <- TRUE   # Run the macro called "MyMacro": xlApp$Run("MyMacro")  # Close the workbook and quit the app: xlWbk$Close(FALSE) xlApp$Quit()  # Release resources: rm(xlWbk, xlApp) gc() 
like image 73
lebelinoz Avatar answered Oct 19 '22 10:10

lebelinoz