首页 文章

从R运行VBA脚本

提问于
浏览
19

我必须管理涉及R脚本和 VBA-code 的工作流程 . 我想在R中运行该过程(我的大部分代码都是),然后调用 VBA-code 进行特定计算 .

我将在R中为VBA准备输入,在某处写入结果(.csv,数据库),然后在R脚本的其余部分中使用结果 .

最好的当然是将整个代码移到R中,但现在这是不可能的 . VBA-code 相当复杂 . 将其转换为R将是一项具有挑战性的长期任务 .

是否有可能在R中管理这样的工作流程?

2 回答

  • 12
  • 10

    这里's a method which doesn' t需要一个VBscript包装器 . 您需要安装 RDCOMClient

    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()
    

相关问题