简体   繁体   中英

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.

.formulalocal works! (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 ):

之前

Then after applying the code below I had this:

后

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/

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)

The problem might be with your formula. Try this-

=IF(LEN([@Units])>0,[@SalesAmount]-[@DiscountAmount],0)

If this doesn't work, I would try using the .formula method instead of .formulaR1C1.. Is there any reason in particular you are using R1C1 references?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM