How to use Excel's array-formulas for a UDF to read each cell correctly?

前端 未结 2 1737
旧时难觅i
旧时难觅i 2020-12-16 03:38

G\'Day,

I have a question more towards helping myself understand how Excel\'s array-formulas (Control+Shift+Enter) can read each cell dynamically into the formula.

2条回答
  •  一向
    一向 (楼主)
    2020-12-16 04:22

    you need to make you array function read the data into an array, process it and create an output array.
    Then the array function nneeds to be entered in a multi-cell array formula (D3:D7) using ctrl-shift-enter.

    Public Function MakesSound(AnimalName As Range) As Variant
    Dim Ansa() As Variant
    Dim vData As Variant
    Dim j As Long
    vData = AnimalName.Value2
    ReDim Ansa(1 To UBound(vData), 1 To 1)
    For j = 1 To UBound(vData)
        Select Case vData(j, 1)
        Case Is = "Duck"
            Ansa(j, 1) = "Quack!"
        Case Is = "Cow"
            Ansa(j, 1) = "Moo!"
        Case Is = "Bird"
            Ansa(j, 1) = "Tweet!"
        Case Is = "Sheep"
            Ansa(j, 1) = "Ba-Ba-Ba!"
        Case Is = "Dog"
            Ansa(j, 1) = "Woof!"
        Case Else
            Ansa(j, 1) = "Eh?"
        End Select
    Next j
    MakesSound = Ansa
    End Function
    

提交回复
热议问题