c# excel assign cell value to another worksheet

Posted on 2010-11-30
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.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)
                                if (i == 1)
                                    worksheet.Cells[3, 2] = "='" + sheet.Name + "'!B3";
                                    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;

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

Accepted Solution

BuggyCoder earned 500 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:-

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

832 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