There's a problem with this formula with Excel Scenarios in C#

kamalasubha muthukumar 41 Reputation points
2020-12-07T10:45:10.4+00:00

I am using Microsoft.Interop.Excel to collect summaries from different scenarios in a specific worksheet.

C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Test_monte_carlo
{
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
static class Program
{
[STAThread]
static void Main(string[] args)
{
string input_filename = args[0];
string output_filename = args[1];
int test_run = Int32.Parse(args[2]);
Console.WriteLine(input_filename);
double averageS = 0;
for (int i = 0; i < test_run; i++)
{
Excel.Application app = new Excel.Application
{
Visible = true,
DisplayAlerts = false
};
Excel._Workbook workbook = app.Workbooks.Open(input_filename, UpdateLinks: 0, ReadOnly: false);
workbook.Activate();
Excel._Worksheet worksheet = workbook.Worksheets["Budget"];
var watch = System.Diagnostics.Stopwatch.StartNew();
Excel.Scenarios sn = worksheet.Scenarios();
Console.WriteLine(sn.Count);
string resultcells = "B6";
sn.CreateSummary(Excel.XlSummaryReportType.xlStandardSummary, resultcells);
watch.Stop();
workbook.SaveAs(output_filename);
var elapsedS = watch.ElapsedMilliseconds / 1000.0;
averageS = averageS + elapsedS;
workbook.Close(false, Type.Missing, Type.Missing);
app.Quit();
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(app);
Console.WriteLine("Time " + i + ":" + elapsedS);
}
averageS = averageS / test_run;
Console.WriteLine("Average time = " + averageS);
}
}
}

I am facing an error while running with the sample workbook,

Unhandled Exception: System.Runtime.InteropServices.COMException: There's a problem with this formula.
Not trying to type a formula?
When the first character is an equal ("=") or minus ("-") sign, Excel thinks it's a formula:
you type: =1+1, cell shows: 2
To get around this, type an apostrophe ( ' ) first:
you type: '=1+1, cell shows: =1+1
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.Scenarios.CreateSummary(XlSummaryReportType ReportType, Object ResultCells)
at Test_monte_carlo.Program.Main(String[] args) in C:\Users\amduser\source\repos\Test_monte_carlo\Test_monte_carlo\Program.cs:line 51

Any help here, please.
Hereby, I have attached the link where I downloaded the xlsx file.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,576 Reputation points
    2020-12-08T08:58:10.567+00:00

    The second parameter of Scenarios.CreateSummary method (Excel) requires a Range object, so please try:

      sn.CreateSummary(Excel.XlSummaryReportType.xlStandardSummary, worksheet.Range["B6:B6"]);  
    

    46164-1.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful