Using Arrays in cell formula calc? - calc

I create Array named MyArray from A1:A10!
Ex.1.: How can I get Array[2]'s Value?
Ex.2.: How can I do iterate (for(from 1 to 10)) in cell formula?
Ex.3.: How can I find MyArray value V1, which >=2.4?
I tried write in cell formula find(MyArray[i],1,10)=2.4, but it doesn't work!
Please help find answers to these 3 example's questions!

I found this good solution
Instead Range for e.g. A1:A10 I can use MyArray;
Ex.1.:
=INDEX(MyArray;2;1)
=INDEX(A1:A10;2;1)
Ex.2.: Need to use =LOOKUP() function with =Match() function
Ex.3.: Need to use =LOOKUP() function with =Match() function.

Related

Output of formula as cells for Excel's Indirect()

I'm using the INDIRECT() function to dynamically refer to worksheets. It seems to me that the function requires a known cell address as input of the formula in quotation marks. However, since I don't know the cell address in advance, I would like to input a cell address that is an output of another formula (e.g., VLOOKUP() or INDEX(MATCH()) ). Is there a way to do this?
Yes, you can compose the string required by INDIRECT() by concatenating formulas that return values that look like a cell address or parts of a cell address.
For example, if your Vlookup or your Index/Match formula returns "A1", then you can use that like
=Indirect(Vlookup(this,there,2,false))
Or, if one formula returns "A" and the other formula returns "1", then combine the output of the two formulas with the & sign.
=Indirect(Vlookup(columnLookup,Table,2,false)&Vlookup(RowLookup,Table,2,false))

Convert Excel Formula to VBA

I have this formula that looks at various criteria across multiple columns and checks to see that if all the all the criteria match, it will paste data from one column to another. I've tried a couple ways to get it into VBA, but I can't seem to get anything to work. Thanks!
=INDEX($D$2:$D$1112,MATCH(1,($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3),0))
You are not going to be able to use that array formula to directly return a value to a cell. VBA does not process an array formula the way that the worksheet can. The best method is to use the worksheet's processing or one of the Application Evaluate methods.
Your lack of a worksheet to reference troubles me. When a formula is in a worksheet cell, it knows what worksheet it is on. When using formulas within VBA, the parent worksheet is a 'best guess' without explicit worksheet referencing.
Here are three methods to put the results from that array formula into Z2:Z4 on the active worksheet. Remember that these cell references should be modified to include the worksheet name.
With ActiveSheet
'this simply puts the formula into the worksheet then reverts the cell from the formula to the returned formula value
.Range("Z2").FormulaArray = "=INDEX($D$2:$D$1112, MATCH(1, ($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3), 0))"
.Range("Z2") = .Range("Z2").Value
'this uses the 'square bracket' method of evaluating a formula on-the-fly
'the formula being evaluated can be array or non-array
'this method is does not like building a formula string from pieces of text
.Range("Z3") = [INDEX($D$2:$D$1112, MATCH(1, ($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3), 0))]
'similar to the method directly above, Application.Evaluate does just that.
'the formula being evaluated can be array or non-array
'this method is easier to build a formula string from pieces of text
.Range("Z4") = Application.Evaluate("INDEX($D$2:$D$1112, MATCH(1, ($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3), 0))")
End With
You need 2 changes:
(1) To use a function in VBA when it is available in native Excel, you need to preface each function with Application.WorksheetFunction. ie:
x = Application.WorksheetFunction.Sum(y,z)
(2) To reference a cell within a sheet, in VBA, you need to access it specifically, in one of a few ways. The simplest for our purposes is the RANGE property, as follows:
x = Application.WorksheetFunction.Sum(Range("A1:A2"))
So to put those two changes together, your formula would look like this:
=Application.WorksheetFunction.INDEX(Range("$D$2:$D$1112",Application.WorksheetFunction.MATCH(1,(RANGE("$A$2:$A$1112"=RANGE("$U$7")*(Range("$C$2:$C$1112"=Range("$W$7")*(Range("$B$2:$B$1112"=Range("F3"),0))
Although I see now having gone through this that you seem to be using an Array Formula - not sure if any special jigging is required to get that to work.

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

Spreadsheetlight Formula not working

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)");

Use formula as a string

I one cell I have to use several different formula depending on certains case.
I another sheet named Static, I wrote a Matrix of formulas in TEXT for all the cases.
My matrix have Criteria1 in rows Criteria2 in columns
So with INDEX/MATCH i will get the text formula for every scenario Criteria1/Criteria2.
Now I want to Evaluate this formula in text.
I did a custom function in VBA
Function Evalue(ByVal str As String)
Application.Volatile
Evalue = Evaluate(str)
End Function
It works when the function is in one block like SUM CONCATENATE etc... but not when there operation of function + -
Your UDF works for me........in A1 enter the text string:
sum(B2:B5)+sum(C4:C9)
in another cell:
=Evalue(A1) displays the correct value.