0
votes

I want to built an array formula that accepts separated values of a single cell as input:

Cell A1: 1;3;4

Cell A2: ={Sum(If(A3:F3)=A1,A4:F4))}

How can I tell excel to intepret the string in A1 as array? I tried to set up a custom vba function, but I was not able to return the vba array correctly to the excel formula.

Can anyone help?

Screenshot

What I tried with VBA:

Function Matrix(vector)
Dim arr As Variant

arr = Array(Split(vector, ";"))
Matrix = arr


End Function

In the above formula I replace A1 by Matrix(A1). But the values of the vectors are returned as strings instead of integers (sorry, actually using a german Excel):

Screenshot

Thanks Peter

1
See my Edit. Thanks!PeterLupo
Split returns an array, so what is the point of Array in Array(Split(vector, ";"))?John Coleman

1 Answers

2
votes

Some clever so-and-so will provide you with a formula I expect, but here is a VBA solution. You need to pass two arguments (at least) I think - the array and look-up range.

Function Matrix(vector, r As Range) As Variant

Dim arr As Variant, i As Long, v As Variant

arr = Split(vector, ";")

For i = LBound(arr) To UBound(arr)
    v = Application.Match(Val(arr(i)), r.Rows(1), 0)
    If IsNumeric(v) Then Matrix = Matrix + r(2, v)
Next i

End Function

The formula in C1 is

=matrix(A1,A3:D4)

Note that Split returns an array.

enter image description here