Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default UDF Array function

I'm not quite sure how an array formula using concatenate would work. I would
think that if you were concatenating strings, you'd want a string when you're
done. (And the results in just one cell.)

But say you wanted to extract just the values in a range that all started with a
lower case a.

You could do something like this:

Option Explicit
Function myArray(rng As Range) As Variant

Dim myCell As Range
Dim iCtr As Long
Dim myTempArray() As String

iCtr = -1
For Each myCell In rng.Cells
If Left(myCell.Value, 1) = "a" Then
iCtr = iCtr + 1
ReDim Preserve myTempArray(0 To iCtr)
myTempArray(iCtr) = myCell.Value
End If
Next myCell

If iCtr < 0 Then
myArray = CVErr(xlErrRef)
Else
If Application.Caller.Rows.Count = 1 Then
myArray = myTempArray
Else
myArray = Application.Transpose(myTempArray)
End If
End If

End Function

Your function returns a variant--this can include an array.

You could use this function by selecting (say) c1:c30 and putting this in C1 and
hitting shift-ctrl-enter:

=myArray(a1:a30)

Some way you build an array and then you plop it back into a bunch of cells. Or
even one cell. Try that same formula, but only select D1. Still hit
ctrl-shift-enter, but this time, hit F2, then F9 to see what's really in the
cell.

onedaywhen wrote:

I want to write my own array function (UDF). I'm talking about the
type of function that takes an array as an argument, is "array
entered" with ctrl+alt+shift and returns a single value.

Say I wanted to write an array version of the native worksheet
function CONCATENATE which is *NOT* an array function i.e. array
entering has not effect:

{=CONCATENATE(A1:A9)}

would only return A1.

So here's a UDF that will do the job:

Public Function MyConcatenate(ByRef Rng As Variant) As String

Dim strConcat As String
Dim rngCell As Range

For Each rngCell In Rng

strConcat = strConcat & rngCell.Text

Next

MyConcatenate = strConcat

End Function

It works for a range of cells but it clearly isn't an array formula.

At this point I realised that, although I know how to use one, I don't
know how to write one so really I don't know what an array formulas
is!

Can anyone explain the internal workings of an array formula? It would
greatly help my understanding if someone could post an array version
of the CONCATENATE function.

Many thanks.


--

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
Array function - I think! CES Excel Worksheet Functions 8 March 30th 10 06:11 PM
Array Function? Johnnie[_2_] Excel Worksheet Functions 4 July 21st 09 08:38 PM
OR function in array-entered IF function veggies27 Excel Worksheet Functions 8 March 11th 08 06:32 PM
Help for Array function [email protected] Excel Worksheet Functions 1 November 28th 06 01:21 AM
AND in an array function? Vince Excel Worksheet Functions 2 April 17th 06 05:46 PM


All times are GMT +1. The time now is 04:31 PM.

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

About Us

"It's about Microsoft Excel"