简体   繁体   中英

How to run the regression function (or any tool/built-in macro) in microsoft excel using C#

I would like to know how to run the regression function in microsoft excel using c#. Currently i am following this tutorial to automate regression in excel:

http://www.michaelcodes.net/2018/09/using-c-to-automate-linear-regression.html

Everything works fine, but the function that i am using (x1 in the tutorial as the variable):

myExcelInteropApplication.Run(@"Regress", sheet.Range($"$A$1:$A${rowCount}"),
                sheet.Range($"$B$1:$B${rowCount}"), false, true, Type.Missing, sheet.Range("$D$1"),
                true, false, false, false, Type.Missing, false);

Throws the following error:

Exception thrown: 'System.Runtime.InteropServices.COMException' in System.Dynamic.dll
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Dynamic.dll
Cannot run the macro 'Regress'. The macro may not be available in this workbook or all macros may be disabled.

I have checked that the data analysis tab is usable in excel and i can successfully execute the regression function within excel itself manually. The rest of the program can also open excel, insert into the cells and close it.

My hypotheses is that the macro im trying to call is misspelled or that something within excel is preventing me from calling the tool/macro.

I am having trouble finding a reference sheet or something recording the macro/tool names, so any example would be appreciated.

Thanks in advance!

You need to reference the name of the.xlam file that has the macro you are trying to run. For what you are trying to do, I think it should be something like:

myExcelInteropApplication.Run("ATPVBAEN.XLAM!Regress", sheet.Range($"$A$1:$A${rowCount}"),
                sheet.Range($"$B$1:$B${rowCount}"), false, true, Type.Missing, sheet.Range("$D$1"),
                true, false, false, false, Type.Missing, false);

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