Trouble computing Excel array formulas programmatically - excel

I am writing values in an excel sheet using a MATLAB program. I am also writing values in cells using formulas (for e.g. the MATLAB program writes =AVERAGE(A1:A10) to a cell and this gets converted to appropriate value (i.e. when I open the sheet, I don't see the above formula text, rather the value).
However I am having trouble writing array formulas (ones with curly braces around). Usually a user enters them by pressing Ctrl+Shift+Enter combination, curly braces appear and appropriate value is computed. However when I write these formulas enclosed in curly braces from MATLAB program the value is not computed, I simply see the formula text (with curly braces around it).
It seems I am not able to simulate Ctrl+Shift+Enter effect by simply writing the array formula to a cell.
Is there any solution to this?

Excel auto-converts values that start with = into .formulas.
To save an array formula, you need to write directly to the FormulaArray property of the range.
More Details:
If you're using a normal formula then you have two options in VBA. You can set the cell's value or formula properties like this:
Range("A3").value = "=Sum(A1:A2)"
Range("A3").Formula= "=Sum(A1:A2)"
When using array formulas, you cant use either of these approaches, you need to store it into a different area of the cell:
Range("A3").FormulaArray = "=Sum(A1:A2)"
When the user pushed Ctrl+Shift+Enter they are telling excel to send the .value of the cell into the .FormulaArray section of the cell.
Otherwise by default when excel sees the = sign i assigns the .value into the .formula
Anyways, this is all relative the the excel object model even if it's in VBA.
Now that I've gone into that detail I believe that matlab's xlswrite function only writes to the value of cells, and cant write to this sub-property, I may be wrong.
Something liek this may work in MATLAB (untested)
mlrange='A3'; % or similar
Excel = actxserver ('Excel.Application');
Excel.Workbooks.Open(filename);
TargetSheet = get(Excel.sheets,'item','A');
TargetSheet.Activate
ran = Excel.Activesheet.get('Range',mlrange);
ran.FormulaArray = '=Sum(A1:A2)'

Related

Excel Exact Function Not Working

When I'm trying to use the and(exact()) function on Excel to see if the values match across a range, if my range is across the same row, I'm getting a formula error whereas if my range is across the same column, I'm getting a TRUE/FALSE output, but it's wrong.
Does anyone know why I'm getting the wrong output?
Short Answer
It is because this is an array formula. You must press Ctrl+Shift+Enter on the keyboard after typing this formula rather than just pressing Enter.
Long Answer
Since this is a pretty simple formula, you can actually use alternatives that are not array formulas.
For example instead of:
= AND(EXACT(A4:A6,A4))
You can do this:
= SUMPRODUCT((A4:A6<>A4)+0)=0
And instead of:
= AND(EXACT(A2:D2,A2))
You can do this:
= SUMPRODUCT((A2:D2<>A2)+0)=0
The formulas have the same final result, but those with SUMPRODUCT are not array formulas. (This is in this specific case only. It is possible to have an array formula that contains SUMPRODUCT.)

Using excel vba, how to write a formula in a cell

In Excel, A1 is equal to =B1/ BDP(C11&”Corp”, “ds036”)
Which BDP(Corp, ds036) is a function and parameters from Bloomberg.
How to use Excel VBA for this formula?
I was trying different ways in VBA to solve my point. One of them is like the line below,
Cells(1,1)=“B1/ BDP(C11&”Corp”, “ds036”)”
An other way I tried, to simplify,
For i=1 to10
Cells(i,1)=“Cells(i,2)/ BDP(cells(i,3)&”Corp”, “ds036”)”
Next
Also, if it can access directly to BDP function. That will be perfect!
try:
Cells(1,1).Formula = "=B1/ BDP(C11&""Corp"", ""ds036"")"
Note:
I used a different flavor of double quote
I doubled-up on the double quotes
Does this do what you want?
Range("A1:A10").Formula = "=B1/BDP(C3&""Corp"",""ds036"")"
If you assign a formula to a multi-cell range, I think Excel will automatically adjust relative cell references for subsequent rows/columns - similar to CTRL+D or dragging down.
This means you don't need to loop through each row and re-construct the formula string for each loop iteration.

Excel SUMIFS Multiple Criteria Cell Value

Similar questions to this have been asked but not exactly like this. There is one that is very close, but that solution is not working for me.
I have a function which returns comma separated values into a cell. I would like to pass the elements in that cell as criteria to a SUMIFS function using an approach like this one.
My attempt is pictured below:
I believe that this is somehow tied to the way that the function is understanding what is in cell G8. It looks like it is adding some extra quotes. If I highlight G8 in the formula bar and press F9, I get:
There are extra quotes on each side of each criteria.
I am open to a custom VBA function solution, but I would prefer something which can be built as a worksheet function. The criteria are returned from a custom VBA function that pulls elements out of a list box and does some regex work to remove extra commas. The number of elements that can be selected is variable so I would like to avoid having to split the criteria into more than one cell. Thanks.
Seems that the raw comma-separated criteria is in G6, All you need is to split this criteria into an array and feed it to SUMIFS. Splitting is available in VBA, but not exposed to Excel. All we need is to write a little UDF that does the splitting of the CSV and use it in our formula:
Function splitCSV(str As String)
splitCSV = Split(str, ",")
End Function
Now the formula in F10 would be:
=SUM(SUMIFS(C3:C10, B3:B10, "blue", A3:A10, splitCSV(G6)))
EDIT
The above is an array formula (Ctrl+Shift+Enter). To have it a normal formula we can use SUMPRODUCT instead of SUM. This leads to more flexibility (normal formula vs array formula) as well as some "expected" performance improvement.
=SUMPRODUCT(SUMIFS(C3:C10, B3:B10, "blue", A3:A10, splitCSV(G6)))

An excel formula that returns from text an excel formula

In my spreadsheet I have certain formulas that give as output texts of the form: A1+B3+D9.
These are cell references, the cell rows and columns are calculated automatically within the original formula.
I would like this to be a formula that then gets evaluated. At present I simply copy-special paste as value and then by hand put an = at the front.
Is there an excel command that does this automatically? i.e. takes a text input, interprets the input as an excel formula and hence evaluates it accordingly.
Can you tweak the formula to get something like
=Indirect(A1) + Indirect(B3) + Indirect(D9)
otherwise, there is no way to get it without VBA.

Obtain entire array from a single cell that is part of a collection of cells containing the same array formula?

It's probably best if I explain this with an example...
Say we have in a worksheet cells A1:C1 containing the values [1,2,3].
I want to take the square of each of these numbers but I do this using a cell-formula so in cell A2 I have {=A1:C1^2}. This is an array-formula. However, if I only press ctrl+alt+enter in cell A2, I see the first element of the array squared (i.e. A1^2 = 1^2 = 1).
If I select all of A2:C2 hit ctrl+alt+enter it expands out the entire array-formula across the three cells and we see the result of [1,4,9].
What I want is to only have to place the array formula in cell A2 and write VBA to be able to access the entire array for the entire operation (i.e. A1:C1^2).
I don't want to execute the function again in VBA as the array must already be stored in memory when we execute {=A1:C1^2} in cell A2 so how can I get the entire array result from a single cell that is part of a larger array?
Note: this is just an example, I plan to do this for large, unknown sized arrays.
Excel cells can only contain scalar values, so what you want to do is not possible without using EVALUATE in your VBA to execute the formula from A2.
And you need to be careful how you use EVALUATE: it has many peculiar quirks.
(see my blog post on EVALUATE )
Excel computes the entire array and displays what it can. This is based on what cells the user selects prior to the array confirmation.
Charles is correct in his answer. There is no way to get at the array without recalculating it with Evaluate(), like so:
MsgBox Join(Evaluate([a2].Formula))
VBA does have the HasArray property for the range object, and in your example with [a2] being the sole cell in the array formula, the HasArray property will return True.
Unfortunately, there is no corresponding GetArray property, which is exactly what you are after.
My take is that Excel's worksheet calculation engine does indeed maintain the array in memory, but this is a separate construct form the range and cells and VBA has no access to it.

Resources