Formula always returns zero

Posted by: mikedempsey on 8 September 2017, 12:41 pm EST

  • Posted 8 September 2017, 12:41 pm EST

    I am trying to display the sum of any numeric cells within the block of cells selected by the user. (Similar to Excel)


    This works using my old app with the COM version of Spread, but it always displays '0.00' in the .Net 3.0 version.


    There are no failures in the code and the formula appears to be correct when I check it in debug mode - 'SUM(R0C1:R15C1)'  for example.  (I did tell it to use R1C1 notation rather than A1 notation, so that's not it.)


    I have attached the code I added to the SelectionChanged event. Any pointers as to what might be wrong? 

    Private Sub Answer_SelectionChanged(ByVal sender As Object, ByVal e As FarPoint.Win.Spread.SelectionChangedEventArgs) Handles Me.SelectionChanged
    'TODO: Display the SUM if a block is selected

    If e.Range.ColumnCount <> 1 Or e.Range.RowCount <> 1 Then
    Dim
    r As Integer = Sheets(0).RowCount
    Dim cr As Model.CellRange = e.Range
    'Row and rowCount etc can be -1 for whole Row/column

    If cr.RowCount = -1 Then _
    cr = New Model.CellRange(0, cr.Column, r, cr.ColumnCount)
    If cr.ColumnCount = -1 Then _
    cr = New Model.CellRange(cr.Row, 0, cr.RowCount, Sheets(0).ColumnCount)
    Sheets(0).AddRows(r, 1)
    Sheets(0).Cells(r, 0).Locked = False
    Sheets(0).Cells(r, 0).CellType = New CellType.NumberCellType()
    Sheets(0).Cells(r, 0).Formula = "sum(R" & cr.Row + 1 & "C" & cr.Column + 1 & _
    ":R" & cr.Row + cr.RowCount & "C" & cr.Column + cr.ColumnCount & ")"
    Sheets(0).Recalculate()
    MainFrm.SetStatus(Sheets(0).Cells(r, 0).Text) '<<< Always zero!

    Sheets(0).RemoveRows(r, 1)
    End If
    End Sub
    Thanks 
  • Replied 8 September 2017, 12:41 pm EST

    Hello,


    A couple of issues I see. The R1C1 format is 1 based index where the rows and columns in the Spread are 0 based. Since you are adding a row before the cells being selected, you need to add 2 to the row properties of the selection instead of 1. The other thing is to make sure all cells in the selection being summed are a number (5) and not a string representation of a number("5").

  • Replied 8 September 2017, 12:41 pm EST

    The row I am adding is at the end of the spreadsheet so adding 1 (to allow for the 1 based formulas) should be correct.


    Maybe the problem is due to numeric 'strings'. I defined the entire sheet as numeric but loaded it from a file using the LoadFile [or whatever it is called] method. If that method resets the cell types it might explain it. (I know the LoadExcel method resets everything but i didnt thing the loadFile did.)


    I am also assuming that if there are non numeric cells in the range they will simply be treated as zeros (as in the COM version) - they will not cause it to return zero. [That was not the case in my tests however - all cells containd 'numbers'.] 

  • Replied 8 September 2017, 12:41 pm EST

    Hello,


    If you are using the LoadTextFile method, only the data is removed from the Spread before loading the text. Then, the text file is read in as string values into the cells. If you set the dataonly parameter of this method to false and there is a CellType assigned to the cell, then the Parse method for that celltype is called to parse the data for the DataModel. Otherwise the data will be put in the DataModel as strings.


    If you have a NumberCellType, then the string numbers being read in from the text file would be parsed into numbers.


    If you are using the Open method to load files into the Spread, then the CellType information will be resetted and read in from the file you are loading. The data is loaded in the same format as it is stored in the XML file. If you have a number stored as a string, then it will look like a number in the Spread, but act like a string in the DataModel and mess up your formulas.

  • Replied 8 September 2017, 12:41 pm EST

    I assume you mean to set the 'unformatted' parameter to False. (There is no 'dataonly' parameter.)


    I had assumed that 'unformatted' simply prevented it from applying formats - such as thousand separators, $ signs etc - so I specified True, since I did not require those.  I did not realize that it meant it would treat the data as strings. (The help is not very useful on this (many) topics.)


    After I set the parameter to False the formula works fine, thanks.


     

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels