Array Constants in Excel

人走茶凉 提交于 2019-12-05 20:11:57

This short VBA UDF should do the job.

Public Function ToArray(rngCell As Range) As Variant

    Dim sFormString As String
    sFormString = rngCell.Formula

    Dim adReturn() As Double
    ReDim adReturn(1) As Double
    If Not Len(sFormString) - 3 > 0 Then
        ToArray = adReturn
        Exit Function
    Else
        sFormString = Mid(sFormString, 3, Len(sFormString) - 3)
    End If

    Dim vTest As Variant
    vTest = Split(sFormString, ",")

    ReDim adReturn(LBound(vTest) To UBound(vTest)) As Double

    Dim iArrayCounter As Integer
    For iArrayCounter = LBound(vTest) To UBound(vTest)
        adReturn(iArrayCounter) = vTest(iArrayCounter)
    Next iArrayCounter

    ToArray = adReturn

End Function

(If the string with the curly brackets is in cell b2 for example, all you need to write in another cell is =sum(toarray(b2)) )

A cell is limited to a single number, string, logical or error value. A single cell cannot contain an array. When the formula "={1,2,3}" is evaluated in a single cell formula, the cell will get only the first value from the array.

You can make the array constant a named array constant, by defining a name (for example: test) like so:

={1,2,3}

then reference the name

=SUM(test)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!