Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

I made an simplified example to show you what I mean.

I created a small fictional farm that has some animals, listed by names and will provide the sound of what the animal makes. In the next column I made a User Defined Function called MakesSound which takes an input of what animal it is and respond the sound that animal it makes. As shown in the snapshot picture below.

enter image description here

Unfortunately, I thought an arrayformula could pick up that I have different cells listing the animals and it ends like this snapshot instead.

enter image description here

so how can I ask the arrayformula to recognise I have different cells in Column B as I know Quacks isn't the answer for other animals. :-)

Here is another snapshot showing the formulas next to arrayformulas for comparsion and code I used as well.

enter image description here

Public Function MakesSound(AnimalName As String) As Variant
    Select Case AnimalName
        Case Is = "Duck"
            MakesSound = "Quack!"
        Case Is = "Cow"
            MakesSound = "Moo!"
        Case Is = "Bird"
            MakesSound = "Tweet!"
        Case Is = "Sheep"
            MakesSound = "Ba-Ba-Ba!"
        Case Is = "Dog"
            MakesSound = "Woof!"
        Case Else
            MakesSound = "Eh?"
    End Select
End Function

I'm open to suggestions.

Thanks, Peter.

like image 323
Peter M Taylor Avatar asked Feb 02 '13 11:02

Peter M Taylor


People also ask

How do I apply an array formula to multiple cells?

To enter a multi-cell array formula, follow these steps: Select multiple cells (cells that will contain the formula) Enter an array formula in the formula bar. Confirm formula with Control + Shift + Enter.

How do you use array formulas and functions in Excel?

Create an array formula that calculates a single resultClick the cell in which you want to enter the array formula. Enter the formula that you want to use. Array formulas use standard formula syntax. They all begin with an equal sign (=), and you can use any of the built-in Excel functions in your array formulas.

How can you recognize an array formula in Excel?

When you select such a cell(s), you can see the braces in the formula bar, which gives you a clue that an array formula is in there. Manually typing the braces around a formula won't work. You must press the Ctrl+Shift+Enter shortcut to complete an array formula.


1 Answers

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
like image 192
Charles Williams Avatar answered Oct 07 '22 01:10

Charles Williams