Home » excel » excel – INDEX+MATCH ArrayFormula Return 1004

excel – INDEX+MATCH ArrayFormula Return 1004

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve been using StackOverflow for months and it has answered all of my questions, expect for the issue I’m currently having. I can’t seem to fix the issue, no matter the options posted on this site. Maybe you can see the issue, that I’m not seeing at the moment.

I have one FormulaArray in VBA that takes the index of an external file, matches one value (Q2) with a value in column B of the external file. Then it matches a second value (P2) with a value in column D of the external file. If there’s a match on both, it’ll return a value that is in column C of the external file.

Works great in Excel, but the moment I create a Selection.FormulaArray = this formula, I get a 1004 in return. The line itself is shorter than 255 characters and even breaking up the INDEX and MATCH formulas into two parts (by Dim formulapart1 and formulapart2 as string) it doesn’t work.

=INDEX([ISOCODE.xlsx]ISOCODE!$A:$D;MATCH(1;([ISOCODE.xlsx]ISOCODE!$B:$B=Q2)*([ISOCODE.xlsx]ISOCODE!$D:$D=P2);0);3)

I tried the following solution that was posted on this website that worked for others, but it’s not working for me, no matter what I do:

Dim theFormulaPart1 As String
Dim theFormulaPart2 As String

theFormulaPart1 = "=INDEX([ISOCODE.xlsx]ISOCODE!$A:$D;XX"
theFormulaPart2 = "MATCH(1;([ISOCODE.xlsx]ISOCODE!$B:$B=Q2)*([ISOCODE.xlsx]ISOCODE!$D:$D=P2);0);3)"

With ActiveSheet.Range("DS2")
    .FormulaArray = theFormulaPart1
    .Replace "XX", theFormulaPart2
End With
How to&Answers:

There are issues with how you have split the code (your theFormulaPart1 is not a valid formula on its own!) but that’s not the real issue here – that’s your “XY Problem”.

The real issue is that when putting a Formula in via VBA, unless you are using .FormulaLocal, you must use the English localisations – this means using , intead of ; to separate the arguments. And, there is no .FormulaArrayLocal, so for an Array formula you always use the English localisation:

ActiveSheet.Range("DS2").FormulaArray = "=INDEX([ISOCODE.xlsx]ISOCODE!$A:$D,MATCH(1,([ISOCODE.xlsx]ISOCODE!$B:$B=Q2)*([ISOCODE.xlsx]ISOCODE!$D:$D=P2),0),3)"

Answer:

Whenever working with excel-VBA (or with anything in general), try always to start from something smaller, that works. In your case, remove the opend worksheet and try to make it work with the one you have. Thus, here is something that works:

Sub TestMe()

    Dim theFormulaPart1 As String
    Dim theFormulaPart2 As String

    theFormulaPart1 = "=INDEX(A:D,XX"
    theFormulaPart2 = "MATCH(1,(B:B=Q2)*(D:D=P2),0),3)"

    With ActiveSheet.Range("E1")
        theFormulaPart1 = Replace(theFormulaPart1, "XX", theFormulaPart2)
        .FormulaArray = theFormulaPart1
    End With

End Sub

Once you fix it, go further and include [ISOCODE!.xlsx]