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 7074145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:00:57+00:00 2026-05-28T06:00:57+00:00

I wanted to add formulas to an Excel workSheet. I managed to do so

  • 0

I wanted to add formulas to an Excel workSheet.
I managed to do so with the Formula property.

The problem is that when I open the worksheet in Excel, I can see that the formula works – but I can only see the result in the cell. I can’t see the formula that was calculated in the Formula Bar at the top of Excel.

Obviously if I enter a formula in Excel itself I can see the result in the cell and the formula in the Formula Bar.

Some of my code:

for (int i = 0; i < nOfColumns / 3; i++)
{
    Range cells = workSheet.Range[workSheet.Cells[2, i * 3 + 3], workSheet.Cells[lastRowNumber, i * 3 + 3]];
    cells.FormulaR1C1 = "=IF(EXACT(RC[-2],RC[-1]),TRUE,ABS(RC[-2]/RC[-1]-1))";
}

below is a test code. even after I save the workbook – the FormulaHidden is false and I can successfully retrieve the formula insterted. really frustrated

        Microsoft.Office.Interop.Excel.Application excelApp = null;
        Workbooks workBooks = null;
        Workbook workBook = null;
        Worksheet workSheet;

        try
        {

            excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.DisplayAlerts = false;

            workBooks = excelApp.Workbooks;
            workBook = workBooks.Open(filePath, AddToMru: false);
            workSheet = workBook.Worksheets.get_Item(1);

            int nOfColumns = workSheet.UsedRange.Columns.Count;
            int lastRowNumber = workSheet.UsedRange.Rows.Count;

            Range rng = workSheet.Range["C1"];
            rng.Formula = "=SUM(B2:B4)";
            String formula = rng.Formula; //retrieve the formula successfully

            rng.FormulaHidden = false;
            workSheet.Unprotect();

            workBook.SaveAs(filePath, AccessMode: XlSaveAsAccessMode.xlExclusive);

            formula = rng.Formula;  //retrieve the formula successfully
            bool hidden = rng.FormulaHidden;

        }
        catch (Exception e)
        {
            throw;
        }
        finally
        {
            if (workBook != null)
            {
                workBook.Close();
                workBook = null;
            }
            if (workBooks != null)
            {
                workBooks.Close();
                workBooks = null;
            }
            if (excelApp != null)
            {
                excelApp.Quit();
                excelApp = null;
            }
        }
    }

Anyone know how to make the formula shown, when adding the formulas programatically ?

  • 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-28T06:00:58+00:00Added an answer on May 28, 2026 at 6:00 am

    finally !!! figured it out. this behavior is caused by the SaveAs flags.
    changed

    workBook.SaveAs(filePath, AccessMode: XlSaveAsAccessMode.xlExclusive);

    to

    workBook.SaveAs(filePath, AccessMode: XlSaveAsAccessMode.xlShared);

    now the only thing left is to understand what exactly is the different between the two flags. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file selector (which works as needed) but I wanted to add
I have a layout that works that I wanted to add the ability to
I wanted to add a new property to one of my model (table). Basically
hi i am working with c# and excel-2007... i wanted to add a macro
Wanted to add an alias for one of the charsets that PayPal may use
If I wanted to add two natural numbers together that are more then 150
HI guys, I wanted to add an AJAX Event to my Homepage, but it
Found this at MDC but how if I'd wanted to add a private variable
I wanted to add a table and a foreign key to that table. Initially
I wanted to add a contextmenu to a tabitem. But it should only be

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.