Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Array Formula creates error if too may cell chosen

You could try using the iserror statement in your formula:

=if(iserror(MyVowelCountArray($A$1),"",MyVowelCoun tArray($A$1))

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Array Formula creates error if too may cell chosen

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
If Selection.Count <= i Then
i = UBound(VowelCountArray) + 1
ReDim Preserve VowelCountArray(0 To Selection.Count - 1)
For i = i To Selection.Count - 1
VowelCountArray(i) = ""
Next i
MyVowelCountArray = VowelCountArray
End If
End Function

--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"ExcelMonkey" wrote in message
...
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Array Formula creates error if too may cell chosen

First, it's ctrl-shift-enter (not alt-ctrl-delete):

And this seemed to work ok for me:

Option Explicit
Function MyVowelCountArray(r As String) As Variant
Dim VowelCountArray() As String
Dim myCount As Long
Dim i As Long
Dim Ch As String
Dim MaxCells As Long

MaxCells = Application.Caller.Cells.Count
ReDim VowelCountArray(1 To MaxCells)

myCount = 0
For i = 1 To Len(r)
Ch = UCase(Mid(r, i, 1))
If Ch Like "[AEIOU]" Then
myCount = myCount + 1
If myCount MaxCells Then
Exit For 'not enough room
End If
VowelCountArray(myCount) = Ch
End If
Next i

If myCount MaxCells Then
MyVowelCountArray = CVErr(xlErrRef)
Else
For i = myCount + 1 To MaxCells
VowelCountArray(i) = ""
Next i
MyVowelCountArray = VowelCountArray
End If

End Function




ExcelMonkey wrote:

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


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formula to point to another cell if an option is chosen. Peggy Excel Worksheet Functions 2 January 30th 08 08:28 PM
Spacebar creates value error Dennis1188 Excel Discussion (Misc queries) 5 March 4th 07 04:58 PM
VLOOKUP post UDF (#DigitsFirstID) creates error bactfarmer Excel Worksheet Functions 0 January 26th 07 08:49 AM
Tricky array formula issue - Using array formula on one cell, then autofilling down a range aspenbordr Excel Programming 0 July 27th 05 03:59 PM
Combobox creates error Stephen Excel Worksheet Functions 0 January 23rd 05 10:55 AM


All times are GMT +1. The time now is 11:07 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"