View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
ExcelMonkey ExcelMonkey is offline
external usenet poster
 
Posts: 553
Default Array Formula creates error if too may cell chosen

When using a UDF in Excel as an Array formula (CTRL-ALT-DELETE), how do you
error handle to avoid getting #N/A in cell ranges which are larger than
results of the array. See example below.

Thanks


Function MyVowelCountArray(r As String) As Variant
Dim VowelCountArray As Variant
'Dim r As String
'This counts the number of vowels
'in a string and returns the found
'vowels in an array

'For example type in "alphabet" in A1
'Then in B2:E2 type in MyVowelCountArray($A$1)
'and hit CTRL-ALT-DELETE
'you will see "a" "a" "e" "#N/A" in these cells

ReDim VowelCountArray(0 To 0)

Count = 0
For i = 1 To Len(r)
Ch = UCase(Mid(r, i, 1))
If Ch Like "[AEIOU]" Then
Count = Count + 1
ReDim Preserve VowelCountArray(0 To Count - 1)
VowelCountArray(Count - 1) = Ch
End If
Next i
MyVowelCountArray = VowelCountArray
End Function