Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c# excel assign cell value to another worksheet

Posted on 2010-11-30
1
Medium Priority
?
1,081 Views
Last Modified: 2012-06-21
I am creating an excel workbook by copying a template sheets from another workbook and renaming them.  What I need to do is sum all the created sheets on the first sheet.  When I assign the value to the cell(worksheet.Cells[3, 2]), the cell's value is System.__ComObject.  I don't even set the value of the child cell in code, it is a formula in the template.
This is my code:

                        Worksheet worksheet = ((Worksheet)originalWorkbook.Worksheets["Division Template"]);
                        worksheet.Copy(Missing.Value, previous);
                        worksheet = ((Worksheet)workbook.Worksheets["Division Template"]);
                        worksheet.Activate();
                        worksheet.Name = "Division " + ddlDivision.SelectedItem.Text;

                        while (dReader.Read())
                        {
                            previous = writeSU(dReader["SU_ID"].ToString(), dReader["Name"].ToString(), previous);
                        }

                        int i = 0;
                        foreach (Worksheet sheet in workbook.Worksheets)
                        {
                            if (sheet.Name != "Division " + ddlDivision.SelectedItem.Text)
                            {
                                i++;
                                if (i == 1)
                                    worksheet.Cells[3, 2] = "='" + sheet.Name + "'!B3";
                                else
                                    worksheet.Cells[3, 2] = worksheet.Cells[3, 2].ToString() + "+'" + sheet.Name + "'!B3";

                            }
                        }

        private Worksheet writeSU(string SU_ID, string Employee_Name, Worksheet previous)
        {
            Worksheet result;
            numberSUs++;

            result = ((Worksheet)originalWorkbook.Worksheets["SU Template"]);
            result.Copy(Missing.Value, previous);
            result = ((Worksheet)workbook.Worksheets["SU Template"]);
            result.Activate();
            result.Name = SU_ID + " " + Employee_Name;
...
            return result;
0
Comment
Question by:KS_Mis
1 Comment
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 2000 total points
ID: 34242494
to apply formula you need to work with range object. please go through the following MSDN Example, i am sure it will help you:-

http://support.microsoft.com/kb/302084
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question