Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to assign formula to cell range in Excel

Someone else's code in the project, that I am trying to fix up.

listO.Range(i, j).FormulaR1C1 = FormulaMatrix(i, j)

where FormulaMatrix(i, j) is always a String value. Whatever random/test value, I try with, is being assigned successfully, except when it is a formula, eg.

=IF(LENGTH([@Units])>0;[@SalesAmount]-[@DiscountAmount]0)

If I remove the = sign in the beginning of the formula, it gets assigned correctly, but then it's useless, because it's not a formula.

@Units, @SalesAmount, @DiscountAmount are references/names of columns.

So, when assigning a formula, I get an exception HRESULT: 0x800A03EC. I looked up in this answer in order to get explanation and followed some of the instructions there. I determined that my problem is the following: the problem happens due to a function entered in a cell and it is trying to update another cell.

Checked out also this post. I tried quite different (like putting just the formulas without = and then run over again and put the equal signs), but same problem.

I am clueless of how to approach this.

like image 506
Syspect Avatar asked Sep 28 '22 03:09

Syspect


People also ask

Why can't I put a formula in an Excel cell?

Cause: The cell is formatted as Text, which causes Excel to ignore any formulas. This could be directly due to the Text format, or is particularly common when importing data from a CSV or Notepad file. Fix: Change the format of the cell(s) to General or some other format.

Why Excel Cannot formula not working?

The most common reason for an Excel formula not calculating is that you have inadvertently activated the Show Formulas mode in a worksheet. To get the formula to display the calculated result, just turn off the Show Formulas mode by doing one of the following: Pressing the Ctrl + ` shortcut, or.

How do I fix Excel not trying to type a formula?

Make sure Excel is set to show formulas in your spreadsheet. To do this, select the Formulas tab, and in the Formula Auditing group, select Show Formulas. Tip: You can also use the keyboard shortcut Ctrl + ` (the key above the Tab key).


2 Answers

.formulalocalworks! (While .formula, .value and .formular1c1 don't.)

I've just started working with VB.NET and came into a very similar issue. This was my simplified data at first (Table1 in Sheet1):

before

Then after applying the code below I had this:

after

The whole code for the form:

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    '~~> Define your Excel Objects
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim strAddress As String = "C:\Temp\SampleNew.xlsx"
    Dim list1 As Object


    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        '~~> Add a New Workbook (IGNORING THE TWO DOT RULE)
        xlWorkBook = xlApp.Workbooks.Open(strAddress)

        '~~> Display Excel
        xlApp.Visible = True

        '~~> Set the relevant sheet that we want to work with
        xlWorkSheet = xlWorkBook.Sheets("Sheet1")

        With xlWorkSheet

            '~~> Change the range into a tabular format
            list1 = .ListObjects("Table1")

        End With

        list1.range(2, 4).formulalocal = "=IF(LEN([@Month])>5;[@Income]-[@MoneySpent];0)"

        '~~> Save the file
        xlApp.DisplayAlerts = False
        xlWorkBook.SaveAs(Filename:=strAddress, FileFormat:=51)
        xlApp.DisplayAlerts = True

        '~~> Close the File
        xlWorkBook.Close()

        '~~> Quit the Excel Application
        xlApp.Quit()

        '~~> Clean Up
        releaseObject(xlApp)
        releaseObject(xlWorkBook)
    End Sub


    '~~> Release the objects
    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class

@Siddharth Rout helped a lot to build this code, as he owns this awesome site: http://www.siddharthrout.com/

like image 86
ZygD Avatar answered Oct 18 '22 18:10

ZygD


The error might be coming from your current data, respectively, the layout of the sheet. I would suggest you to check what is inside the listO.Range(i, j).FormulaR1C1 before you assign the formula.

I have had a case where the range has already got wrong data inside, and then strangely, I don't know why, I cannot assign the new formula.

If that is the case - try clearing the value of the range and then assigning the formula:

listO.Range(i, j).FormulaR1C1 = ""
listO.Range(i, j).FormulaR1C1 = FormulaMatrix(i, j)
like image 45
Milkncookiez Avatar answered Oct 18 '22 19:10

Milkncookiez