I am struggling trying to let an Excel Array formula to be applied through a VBA code.
The formula is the following:
=IF(B2=VLOOKUP(B2,Admin!$G$5:$G$501,1,FALSE),IFERROR(INDEX(Admin!$G$5:$K$501,MATCH(1,(Admin!$I$5:$I$501=A2)*(Admin!$G$5:$G$501=B2),0),5),INDEX(Admin!$G$5:$K$501,MATCH(1,(Admin!$I$5:$I$501="ALL")*(Admin!$G$5:$G$501=B2),0),5)),"")
I am trying to apply it through VBA using the following code:
Sub LongArrayformula()
Dim ArrayFormulaPart1, ArrayFormulaPart2 As String
ArrayFormulaPart1 = "=IF(B2=VLOOKUP(B2,Admin!$G$5:$G$501,1,FALSE),IFERROR(INDEX(Admin!$G$5:$K$501,XXXX"
ArrayFormulaPart2 = "MATCH(1,(Admin!$I$5:$I$501=A2)*(Admin!$G$5:$G$501=B2),0),5),INDEX(Admin!$G$5:$K$501,MATCH(1,(Admin!$I$5:$I$501=""ALL"")*(Admin!$G$5:$G$501=B2),0),5)),"""")"
With ActiveSheet.Range("bv2")
.FormulaArray = ArrayFormulaPart1
.Replace "XXXX", ArrayFormulaPart2, lookat:=xlPart
End With
End Sub
I know on the web there are many sites covering this subject but I can't let this formula be applied automatically.
I keep getting the error "Unable to set the FormulaArray propoerty of the Range class" and the ".FormulaArray = ArrayFormulaPart1" gets highlighted.
I have tried other codes as well with no luck. Since the syntax is pretty much unknown to me, I am not sure about what to change in order to adapt the many codes I sow to my needs.
Thank you in advance for your help and eventually a quick explanation on how to properly set an array formula using VBA.
That is because the first formula must be a viable formula to be entered in the first place:
Sub LongArrayformula()
Dim ArrayFormulaPart1, ArrayFormulaPart2 As String
ArrayFormulaPart1 = "=IF(B2=VLOOKUP(B2,Admin!$G$5:$G$501,1,FALSE),IFERROR(INDEX(Admin!$G$5:$K$501,9999,5),INDEX(Admin!$G$5:$K$501,MATCH(1,(Admin!$I$5:$I$501=""ALL"")*(Admin!$G$5:$G$501=B2),0),5)),"""")"
ArrayFormulaPart2 = "MATCH(1,(Admin!$I$5:$I$501=A2)*(Admin!$G$5:$G$501=B2),0)"
With ActiveSheet.Range("bv2")
.FormulaArray = ArrayFormulaPart1
.Replace "9999", ArrayFormulaPart2, lookat:=xlPart
End With
End Sub