Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6548939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:01:56+00:00 2026-05-25T12:01:56+00:00

Here is the sample code XL.Worksheet worksheet = Globals.ThisAddIn.Application.ActiveSheet; XL.Range range = worksheet.UsedRange; arrValue

  • 0

Here is the sample code

XL.Worksheet worksheet = Globals.ThisAddIn.Application.ActiveSheet;
XL.Range range = worksheet.UsedRange;
arrValue = (object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);

for (int iRow = 1; iRow <= arrValue.GetLength(0); iRow++)
{
    for (int iCol = 1; iCol <= arrValue.GetLength(1); iCol++)
    {
         if (range.Cells[iRow, iCol].Formula != null)
              range.Cells[iRow, iCol].Formula = "=" + kvp.Value.ToString();  // assign underlying formula (kvp is keyValuePair of dictionary) to the cell

         range.Cells[iRow, iCol].Value = evalResults[count].ToString(); // assign value to the cell 
         count++;
    }

} 

I wonder if Cells.Value and Cells.Formula would overwrite each other. If I want to assign both of them at the same time, what is the best way to do it?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:01:57+00:00Added an answer on May 25, 2026 at 12:01 pm

    It is not possible to assign bot the value and the formula to an excel cell (you can’t do that even using excel in the normal interactive way), since the result would be undefined, i.e. if the result of the formula is different from the value what would you show in the cell?

    Since you want to preserve your formulas for reuse in the future you could have a Recalculate method which applies the formulas, saves them to a dictionary (and you already have something like that, don’t you?), and then set the value of the cells with the current value (calculated from the formula).

    Maybe this is what you are already doing here, but it’s not so clear, since I don’t understand what evalResults is. Anyway, I mean something like this:

    private Dictionary<int,Dictionary<int,string>> formulas = new Dictionary<int,Dictionary<int,string>>(); // this has to be initialized according to your formulas
    public void Recalculate()
    {
        XL.Worksheet worksheet = Globals.ThisAddIn.Application.ActiveSheet;
        XL.Range range = worksheet.UsedRange;
    
        for (int iRow = 1; iRow <= arrValue.GetLength(0); iRow++)
        {
            for (int iCol = 1; iCol <= arrValue.GetLength(1); iCol++)
            {
                range.Cells[iRow, iCol].Formula = "=" + formulas[iRow][iCol];  
                range.Cells[iRow, iCol].Value = range.Cells[iRow, iCol].Value; 
            }
    
        }
    }
    

    If you want to keep the formulas in the excel workbook and not in memory (in the dictionary), a solution could be to have a hidden worksheet where you store the formulas as strings (i.e. without the ‘=’) in order to avoid their evaluation and the performance issues. So you would have a SetFormulas and a Recalculate method very similar to your code:

    public void SetFormulas()
    {
        XL.Worksheet worksheet = Globals.ThisAddIn.Application.ActiveSheet;
        XL.Worksheet formulasWorksheet = Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets["formulas"];
        XL.Range range = worksheet.UsedRange;
        arrValue = (object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);
    
        for (int iRow = 1; iRow <= arrValue.GetLength(0); iRow++)
        {
            for (int iCol = 1; iCol <= arrValue.GetLength(1); iCol++)
            {
                 if (range.Cells[iRow, iCol].Formula != null)
                 {
                      range.Cells[iRow, iCol].Formula = "=" + kvp.Value.ToString();  // assign underlying formula (kvp is keyValuePair of dictionary) to the cell
                      formulasWorksheet.Cells[iRow, iCol].Value = kvp.Value.ToString(); // storing the formula here
                 }
                 range.Cells[iRow, iCol].Value = evalResults[count].ToString(); // assign value to the cell 
                 count++;
            }
    
        }
    }
    
    
    
    public void Recalculate()
    {
        XL.Worksheet worksheet = Globals.ThisAddIn.Application.ActiveSheet;
        XL.Worksheet formulasWorksheet = Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets["formulas"];
        XL.Range range = worksheet.UsedRange;
        arrValue = (object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);
    
        for (int iRow = 1; iRow <= arrValue.GetLength(0); iRow++)
        {
            for (int iCol = 1; iCol <= arrValue.GetLength(1); iCol++)
            {
                 if (!string.IsNullOrEmpty(formulasWorksheet.Cells[iRow, iCol].Text))
                 {
                      range.Cells[iRow, iCol].Formula = "=" + formulasWorksheet.Cells[iRow, iCol].Text; // restoring the formula
                      range.Cells[iRow, iCol].Value = range.Cells[iRow, iCol].Value // replacing the formula with the value
                 }
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's an sample code, where only the string object is released. NSString *nameOfFile =
I am trying to reuse Apple's Speak Here sample code in my own iPhone
Here is my sample code: from xml.dom.minidom import * def make_xml(): doc = Document()
Here is a sample code to retrieve data from a database using the yield
Here is my sample code. It is meant to be an iterative procedure for
Here is a sample code: public class TestIO{ public static void main(String[] str){ TestIO
Here's the sample code: public static void col (int n) { if (n %
Here's the sample code: class TestAO { int[] x; public TestAO () { this.x
I tried to compile Ercia Sadun's sample code here , but this error came
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.