Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 211
Default How to constract an array UDF?

Sure would oblige if one would kindly provide the CODE for developing a UDF
for simple formula of reversing the column values like the following:

=OFFSET($A$1,ROWS($A$1:$A$20)-ROWS($B$1:B1),0)

Thus, if a UDF like the following, is entered in an array form:
{=REVCOL(A1:A20)}
on the range B1:B20, the B1:B20 would represent the data of A1:A20 but in
the reverse order.

Thanx in advance,
Best Regards,

Faraz
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default How to constract an array UDF?

I'm not sure why you would want a UDF to do what you already have a relatively compact formula doing, but here it is...

Function REVCOL(R As Range) As Variant
Dim CellValues As Variant, CallerRow As Long
CellValues = WorksheetFunction.Transpose(R)
CallerRow = Application.Caller.Row
REVCOL = CellValues(R.Count + R(1).Row - CallerRow)
End Function

Note that this is **not** an array formula... just commit it with the Enter key; and don't forget to make the range argument for it with absolute cell references so that when you copy the function down, the cell address doesn't "drift".

--
Rick (MVP - Excel)


"Faraz A. Qureshi" wrote in message ...
Sure would oblige if one would kindly provide the CODE for developing a UDF
for simple formula of reversing the column values like the following:

=OFFSET($A$1,ROWS($A$1:$A$20)-ROWS($B$1:B1),0)

Thus, if a UDF like the following, is entered in an array form:
{=REVCOL(A1:A20)}
on the range B1:B20, the B1:B20 would represent the data of A1:A20 but in
the reverse order.

Thanx in advance,
Best Regards,

Faraz

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 211
Default How to constract an array UDF?

That's the main reason bro!
What I need is to have some knowledge of how to create an array formula?
Simply a construction model not necessarily for Reversing prpose.
It was only an example.
Please c if u can help me in giving some other example of your own as to how
to construct an array formula?

Thanx again!
Looking 4ward 4 your guidance.

Best Regards,

Faraz


"Rick Rothstein" wrote:

I'm not sure why you would want a UDF to do what you already have a relatively compact formula doing, but here it is...

Function REVCOL(R As Range) As Variant
Dim CellValues As Variant, CallerRow As Long
CellValues = WorksheetFunction.Transpose(R)
CallerRow = Application.Caller.Row
REVCOL = CellValues(R.Count + R(1).Row - CallerRow)
End Function

Note that this is **not** an array formula... just commit it with the Enter key; and don't forget to make the range argument for it with absolute cell references so that when you copy the function down, the cell address doesn't "drift".

--
Rick (MVP - Excel)


"Faraz A. Qureshi" wrote in message ...
Sure would oblige if one would kindly provide the CODE for developing a UDF
for simple formula of reversing the column values like the following:

=OFFSET($A$1,ROWS($A$1:$A$20)-ROWS($B$1:B1),0)

Thus, if a UDF like the following, is entered in an array form:
{=REVCOL(A1:A20)}
on the range B1:B20, the B1:B20 would represent the data of A1:A20 but in
the reverse order.

Thanx in advance,
Best Regards,

Faraz

.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default How to constract an array UDF?

On Jan 17, 9:45*am, Faraz A. Qureshi
wrote:
That's the main reason bro!
What I need is to have some knowledge of how to create an array formula?
Simply a construction model not necessarily for Reversing prpose.
It was only an example.
Please c if u can help me in giving some other example of your own as to how
to construct an array formula?

Thanx again!
Looking 4ward 4 your guidance.

Best Regards,

Faraz



"Rick Rothstein" wrote:
I'm not sure why you would want a UDF to do what you already have a relatively compact formula doing, but here it is...


Function REVCOL(R As Range) As Variant
* Dim CellValues As Variant, CallerRow As Long
* CellValues = WorksheetFunction.Transpose(R)
* CallerRow = Application.Caller.Row
* REVCOL = CellValues(R.Count + R(1).Row - CallerRow)
End Function


Note that this is **not** an array formula... just commit it with the Enter key; and don't forget to make the range argument for it with absolute cell references so that when you copy the function down, the cell address doesn't "drift".


--
Rick (MVP - Excel)


"Faraz A. Qureshi" wrote in ...
Sure would oblige if one would kindly provide the CODE for developing a UDF
for simple formula of reversing the column values like the following:


=OFFSET($A$1,ROWS($A$1:$A$20)-ROWS($B$1:B1),0)


Thus, if a UDF like the following, is entered in an array form:
{=REVCOL(A1:A20)}
on the range B1:B20, the B1:B20 would represent the data of A1:A20 but in
the reverse order.


Thanx in advance,
Best Regards,


Faraz

.- Hide quoted text -


- Show quoted text -


Here's a UDF using an array formula:

Function RevCol(ReverseA As Variant) As Variant
Dim NumRows As Long, TempA() As Double, i As Long

If TypeName(ReverseA) = "Range" Then ReverseA = ReverseA.Value2
NumRows = UBound(ReverseA)
ReDim TempA(1 To NumRows, 1 To 1)

For i = 1 To NumRows
TempA(i, 1) = ReverseA(NumRows - i + 1, 1)
Next i
RevCol = TempA

End Function

So the main things to remember a

Declare the range you are operating on and the function as variants
Convert the variant/range into an array with arrayname =
arrayname.value2 (the "If typename = "Range"" bit allows the function
to be used from another VBA routine, passing an array, rather than a
range).
Do what you want to the array
Assign the array at the functions return value.
Enter with Ctrl-Shift-Enter

This sort of array UDF will usually be much faster than a UDF working
on range objects.

These blog articles might help:
http://newtonexcelbach.wordpress.com...es-and-arrays/
http://newtonexcelbach.wordpress.com...-and-arrays-2/

By the way, for the non-VBA solution I'd use:
=INDEX($A$2:$A$21,ROW($A$21)-ROW(A1))
(for data in A2:A21)

Doug
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
Prevent cell/array references from changing when altering/moving thecell/array nme Excel Discussion (Misc queries) 1 September 19th 08 01:53 PM
Redimming an array dynamically assigned from range (how to redim first dimension of a 2-D array? /or/ reverse the original array order) Keith R[_2_] Excel Programming 3 November 13th 07 04:08 PM
meaning of : IF(Switch; Average(array A, array B); array A) DXAT Excel Worksheet Functions 1 October 24th 06 06:11 PM
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Excel Programming 6 November 9th 05 05:54 AM
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Excel Programming 1 November 8th 05 04:21 AM


All times are GMT +1. The time now is 04:09 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"