简体   繁体   中英

Run Excel macro from BERT

I'm trying to run an Excel 2016 macro in BERT. Within the spreadsheet, I can operate the macro as a button called "Button_To_Click". This is the name of the macro in the Excel document's list of macros as well. I'm using the command EXCEL$Application.Run("BERT.Call", "Button_To_Click") and receiving the message Error: attempt to apply non-function .

This is my first time using BERT or anything VBA-related (I'm more familiar with R), and the documentation is unclear to me on how to make macros that were created and stored in Excel work in BERT. A more complete example is below:

wb <- EXCEL$Application$get_ActiveWorkbook();
INsheet <- wb$get_Sheets(1); #get first sheet in active workbook

S = c(155,157,159)
range <- MEMsheet$get_Range( "B10:B10" ); #change B10 to S[1]
range$put_Value(S[1]);

### 'Run Model' button is named 'Button_To_Click'. ###
EXCEL$Application.Run("BERT.Call", "Button_To_Click");

The function to run a macro requires calling the name of the macro and the sheet that hosts it within the workbook. It uses the name of the macro as saved in the macros menu and the action associated with it, not the text written on the clickable button itself. In this case that button was labeled with the text "Button_To_Click", but the actual name of the macro was "CalculateButton", saved in Sheet2, and the action associated with it was "Click". The BERT command Run.Macro() was sufficient as long as the BERT scripts are sourced.

source(file.path("C:/Users/MyName/Documents/BERT2/examples/excel-functions.r" )) #loads special BERT functions needed to have Excel and R interact
Run.Macro ( "Sheet2.CalculateButton_Click") #runs the macro named 'CalculateButton' located in Sheet2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM