I am currently trying to launch a very simple VBA macro from R. For that, I am following the procedure found here : Run VBA script from R Unfortunately, when I open the Excel file after that, it's corrupted and Excel stops. Here is my code :
r:
library(XLConnect)
saveWorkbook(wb,pathfile)
# The saveWorkbook part is working
shell(shQuote(normalizePath(pathtovbs)), "cscript", flag = "//nologo")
Option Explicit
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(pathfile, 0, True)
xlApp.Run "PERSONAL.XLSB!MyMacro"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
vba PESONAL.XLSB!MyMacro:
Sub MyMacro()
Dim ws As Worksheet
For Each ws In Sheets
ws.Range("C:C").EntireColumn.Delete
ws.Range("A:A").EntireColumn.Delete
Next ws
End Sub
Do you have any idea what is going on ? I have checked the path of each file and they are good. Thank you very much in advance.
Edit : Apparently, problem comes from the vbscript. The file opens but it can't find the macro located in my personal library (PERSONAL.XLSB). When I open Excel manually, I can access this macro, but when I open Excel from another program, I can't. Any idea why ?
Pauline