Insert an Array Formula via VBA - 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

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

Calculating Formula After Text Paste

I have a formula that constructs a vlookup function while drawing the file path from a date input from the user, next I have a macro that takes the results of this formula and pastes them in Cell C6.
=("=" & "IF(ISNA(VLOOKUP($A6," & "'I:\CM\PC\A\TR\AT\"&TEXT(C$5,"yyyy")&"\"&TEXT(C$5,"mmmmmmmmmmm")&"\["&TEXT(C$5,"mmm")&TEXT(C$5,"d")&".xlsm]Violations Count'!$A:$B"&",2,FALSE)),"""",VLOOKUP($A6,"&"'I:\CM\PC\A\TR\AT\"&TEXT(C$5,"yyyy")&"\"&TEXT(C$5,"mmmmmmmmmmm")&"\["&TEXT(C$5,"mmm")&TEXT(C$5,"d")&".xlsm]Violations Count'!$A:$B"&",2,FALSE))")
The trouble I am having is finding a way for VBA to activate the formula instead of having it sit there in a text format as:
=IF(ISNA(VLOOKUP($A6,'I:\CM\PC\A\TR\AT\2016\December\[Dec5.xlsm]Violations Count'!$A:$B,2,FALSE)),"",VLOOKUP($A6,'I:\CM\PC\A\TR\AT\2016\December\[Dec5.xlsm]Violations Count'!$A:$B,2,FALSE))
Afterward I just have a macro autofill it down for the other names between column A6:A37.
If anyone can help me with VBA activating the cell, it would be much appreciated.
If you want to get the value of a cell (which might contain a text string) you use something like:
someVariableEtc = Range("A1").Value
If you want to set a Cell's formula using VBA, you would use something like:
Range("C6").Formula = "some text string"
Combining the two, you could come up with something like:
Range("C6").Formula = Range("A1").Value
Look this up. The evaluate function might be what you're looking for. The simplest way to use it is wrapping an expression in square brackets [].

Problems recording long Excel formula with macro recorder

I'm trying to record a macro of my formula in Excel and it gives me a syntax error.
This is the formula:
=IF(AND(OR(B2="toola",B2="toola1",B2="toola2",B2="SFx200"),OR(H2="Q1",H2="Q2",H2="Q3",H2="Q4")),CONCATENATE(H2," "," ",IF(I2="p","pre","")," ","SFx","-",IF(A2="",0,A2)),IF(AND(OR(B2="toolb",B2="toolb1",B2="toolb2",B2="toolb3",B2="yoolb4",B2="toolb4",B2="toolb5",B2="toolb6",B2="toolb7",B2="toolb8",B2="toolb9",B2="toolb10",B2="toolb11",B2="toolb12"),OR(H2="Q1",H2="Q2",H2="Q3",H2="Q4")),CONCATENATE(H2," ",IF(I2="p","pre",""),"-"," ",IF(A2="",0,A2)),IF(AND(OR(B2="up",B2="up1",B2="up2",B2="up3",B2="up4",B2="up5",B2="Up6",B2="up7"),OR(H2="Q1",H2="Q2",H2="Q3",H2="Q4")),CONCATENATE(H2," ","UP","-",IF(A2="",0,A2)))))
I know that it is big, but why won't the macro recorder properly record it?
The macro recorder is useless with long formulas as it overwrites some of the formula string when it adds in line continuations for the VB editor. You'll either need to edit the code yourself or shorten the formula - e.g. use things like OR(H2={"Q1","Q2","Q3","Q4"}) rather than OR(H2="Q1",H2="Q2",H2="Q3",H2="Q4")
Also if possible replace something like this:
OR(B2="toolb",B2="toolb1",B2="toolb2",B2="toolb3",B2="yoolb4",B2="toolb4",B2="toolb5",B2="toolb6",B2="toolb7",B2="toolb8",B2="toolb9",B2="toolb10",B2="toolb11",B2="toolb12")
with this
LEFT(B2,5)="toolb"
When you have more than 7 times if excel does not want to work. Thus, you should go around it. With Select Case.
Or something like this, when the nested ifs are in a formula:
https://www.techonthenet.com/excel/macros/if_custom.php
You can go up to as many cases as you need. Then simply use the custom formula.

How to combine FormulaArray and FormulaLocal options in Excel VBA?

I am sending functions inside VBA code to the cells with a VBA-code line like below:
Sheets("Sheet1").Range("B2").FormulaLocal = "=somefunctions_in_local_language"
I am using FormulaLocal option because the functions in cells are in the local language, not in English.
Now I want to send array functions, and I am supposed to use FormulaArray to do this. However, even the array functions will be in the local language. I guess I am supposed to combine both FormulaArray and FormulaLocal somehow, but how?
I tried to find if there is something like FormulaArrayLocal, but there is not such a thing. So any idea?
That link didn't give a direct answer, but I did find a solution:
Sheets("Sheet1").Range("B2").FormulaLocal = "=somefunctions_in_local_language"
Sheets("Sheet1").Range("B2").FormulaArray = Sheets("Sheet1").Range("B2").Formula
It should only be .Formula in the second line. If you use .FormulaLocal, then you get an error.

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.