How to Execute VBA Code in Excel via R using RDCOMClient
code
rtip
vba
excel
Author
Steven P. Sanderson II, MPH
Published
June 28, 2024
Introduction
Hey everyone,
Today, I want to share a neat way to bridge the gap between R and Excel using VBA. Specifically, we’ll look at how to run VBA code in Excel directly from R. This can be incredibly useful if you’re looking to automate repetitive tasks or leverage the power of VBA while working within the R environment.
We’ll use the RDCOMClient library, which allows R to control COM (Component Object Model) objects, such as an Excel application. If you’ve ever found yourself toggling between R and Excel, this method will streamline your workflow significantly.
What We’ll Do
We’ll write a VBA macro that populates cells A1:A10 with random numbers and then run this macro from R.
Step-by-Step Guide
1. Install and Load RDCOMClient
First, you’ll need to install the RDCOMClient package. It’s not available on CRAN, so you have to install it from the omegahat repository.
Open Excel and press ALT + F11 to open the VBA editor. Insert a new module and add the following VBA code:
Sub FillRandomNumbers()
Dim i As Integer
For i = 1 To 10
Cells(i, 1).Value = Rnd()
Next i
End Sub
This macro fills cells A1 to A10 with random numbers.
3. Run the VBA Macro from R
Now, let’s write the R code to open Excel, run the macro, and then close Excel.
# Load the RDCOMClient librarylibrary(RDCOMClient)# Create a new instance of Excel applicationexcel_app <-COMCreate("Excel.Application")# Make Excel visible (optional)excel_app[["Visible"]] <-TRUE# Add a new workbookwb_path <-"C:\\Users\\steve\\Documents\\GitHub\\steveondata\\posts\\2024-06-28\\vba_rand_from_r.xlsm"workbook <- excel_app[["Workbooks"]]$Open(wb_path)# Reference the first sheetsheet <- workbook$Worksheets(1)# Run the macroexcel_app$Run("FillRandomNumbers")
NULL
# Save the workbook (optional)workbook$SaveAs("C:\\Users\\steve\\Documents\\GitHub\\steveondata\\posts\\2024-06-28\\random_numbers.xlsm")
[1] TRUE
# Close Excelexcel_app$Quit()
NULL
# Release the COM objectrm(excel_app)rm(sheet)rm(workbook)gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 666819 35.7 1477710 79 1122664 60.0
Vcells 1220653 9.4 8388608 64 1770896 13.6
Explanation
Initialize Excel Application: COMCreate("Excel.Application") starts a new instance of Excel.
Make Excel Visible: This step is optional but useful for debugging.
Add Workbook and Reference Worksheet: We create a new workbook and reference the first sheet.
Run the Macro: excel_app$Run("FillRandomNumbers") executes the macro.
Save Workbook: Optionally save the workbook with the generated random numbers.
Close and Clean Up: Close Excel and clean up the COM object to free up resources.
Give this a try and see how you can extend it to your own needs. Whether it’s automating report generation, data cleaning, or complex calculations, integrating R with VBA can enhance your productivity significantly. The RDCOMClient package opens up a world of possibilities for combining the strengths of R and Excel.
Feel free to share your experiences or ask questions in the comments.