Spreadsheetlight Formula not working - spreadsheet

i'm using Spreadsheetlight for creating excel document. I need to use a formula on a specific cell, but it's not working. The code:
report.SetCellValue(string.Format("{0}{1}", Report.CELL_MAP.ACTIVITY_CRU_COL, row), string.Format("=IF({0}{1}=0; 0; ROUND(({2}{1}/{0}{1})*100; 0))", Report.CELL_MAP.ACTIVITY_REAL_MD_VAL_COL, row, Report.CELL_MAP.ACTIVITY_INVOICED_MD_VAL_COL));
Is there something I'm missing? Setting formulas like '=E9' is stored in cell as formula and works in final document. Any ideas why it doesn't work?

Should it be commas instead of semicolons in the formula? Like so:
report.SetCellValue(string.Format("{0}{1}", Report.CELL_MAP.ACTIVITY_CRU_COL, row), string.Format("=IF({0}{1}=0, 0, ROUND(({2}{1}/{0}{1})*100, 0))", Report.CELL_MAP.ACTIVITY_REAL_MD_VAL_COL, row, Report.CELL_MAP.ACTIVITY_INVOICED_MD_VAL_COL));

I have just tested Vincent's answer on SUMPRODUCT and IF formulas and I can confirm that it works.
SUMPRODUCT example:
Instead of
sl.SetCellValue(cellRef, "=SUMPRODUCT(A2:B2;--A1:B1=\"Something\")");
should be
sl.SetCellValue(cellRef, "=SUMPRODUCT(A2:B2,--A1:B1=\"Something\")");
IF example:
Instead of
sl.SetCellValue(cellRef, "=IF(A1=\"Something\";1;0)");
should be
sl.SetCellValue(cellRef, "=IF(A1=\"Something\",1,0)");

Related

Excel formula within VBA

I have quite a few cells that contain formula, then with VBA the outcome of this formula is the value for a variable, like so:
On sheet in cell AS4:
=SUMPRODUCT(MAX((ROW($AE$4:$AE$997))*($AE$4:$AE$997<>"")))
and then in my VBA:
numRows = ws.Range("AS4").Value
However this is starting to get hard to keep track of which cell is feeding which variable, avoiding overwriting those cells on the sheet by accident, etc.
I need to be able to perform this calculation within VBA if I can, removing the need to have "calculation cells" on my sheet.
I have discovered there is a way to use formula with WorksheetFunction, but only found simple examples of this and cannot adapt it to my situation above.
numRows = WorksheetFunction.SumProduct(MAX((ROW($AE$4:$AE$997))*($AE$4:$AE$997<>"")))
Is not going to work...
Is there a way to do this, or am I better scrapping the idea of using formula and using a pure VBA method?
With help from SJR this was the answer:
numRows = [=SUMPRODUCT(MAX((ROW(Weights!$AE$4:$AE$997))*(Weights!$AE$4:$AE$997<>"""")))]
A bit more research taught me that evaluate(" ") can be just replaced with square brackets [ and ]. Although, if I had variables in the mix of this formula or the formula wasn't constant then I would have to use Evaluate.
I also needed to add the sheet name to the formula as this formula was no longer functioning within the sheet and AE4:AE997 was no longer referring to the correct sheet.
Doubling up on quotes is also necessary as it is code and sees " differently to a formula on the sheet

Cell variable inside COUNTIF

I have a column with many words in each cell. I want to count how many times a word is in the column and use:
=COUNTIF(Data!C3:C9,"*word*")
It works fine, now I want to reference a cell instead put the word. I am trying this, but it doesn't work:
=COUNTIF(Data!C3:C9,"*"+A3+"*")
How can I use the cell variable inside the criterion?
In Google Spreadsheet, to concatenate two or more strings use ampersand, that is: "&"
Try the following formula:
=COUNTIF(Data!C3:C9,"*"&A3&"*")
Or you can alternatively try:
=COUNTIF(Data!C3:C9,CONCATENATE("*",A3,"*"))

How to put a formula into a cell by Excel VBA?

I'm trying to put this =IF(D49>0,D49-D50-D51+D52+D53,) into a cell D54 by using macro.
My code is as follow
Range("D54").FormulaR1C1 = "=IF(D52>0,D49-D50-D51+D52+D53,)"
But when I run the macro.
Cell D54 gets this =IF('D52'>0,'D49'-'D50'-'D51'+'D52'+'D53',) instead.
Excel adds quotation marks to each dells in the formula, render the formula useless. How can I get macro to into formula as formula?
I think you are having issues because you are using the R1C1 formula property for your range, yet you are using classical cell references in your formula. You should be using the Range("D54").Formula property. So your code would look like:
Range("D54").Formula = "=IF(D52>0,D49-D50-D51+D52+D53,)"
You could just use Range("D54").Value = "=IF(D52>0,D49-D50-D51+D52+D53,)"
I have used that method before and hand good results with it.

Insert an Array Formula via VBA

I'm using VBA, and I need to insert an array formula (the one that if I'm writing it manually, I'll press Ctrl+Shift+Enter and not just Enter). When I'm inserting it like a regular formula it doesn't work, neither when I put it with {} around it...
What's the correct way of writing that formula using VBA?
The formula is this:
=INDEX(subset!R1C1:R2472C10,MATCH(1,(RC1=subset!C1)*(RC2=subset!C2)*(RC5=subset!C5)*(RC6=subset!C6),0),10)
You're looking for the FormulaArray property that you can set for a cell like so:
Range("A1").FormulaArray = "=INDEX(subset!R1C1:R2472C10,MATCH(1,(RC1=subset!C1)(RC2=subset!C2)(RC5=subset!C5)*(RC6=subset!C6),0),10)"
See the documentation here: http://msdn.microsoft.com/en-us/library/office/ff837104%28v=office.15%29.aspx

Evaluate string as formula in cell in Excel 2007

I haven't had any luck finding an eval-type function to use in a cell for Excel.
Let's say I have a cell A1 that contains string "C4". I'd like to be able to write an in-cell function similar to this IF(EVAL(A1)>10,"TOO BIG","TOO SMALL"). That would then effectively be C4>10 for the expression.
Is this possible without VBA?
If I understand correctly the problem the solution would be:
IF(INDIRECT(A1)>10,"TOO BIG","TOO SMALL")