View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Joe Adams Joe Adams is offline
external usenet poster
 
Posts: 8
Default Unusual Operation of a User Defined Function

I have a variable range, rng_FormsList, which I would like
to sort actively within the worksheet. I have written a
function which somewhat works. I say somewhat because
sometimes it works and other times it does not.
So, I know I have missed something fundimental.

I call the function (listed below) with a range string and
a positional element number to return that is located in a
reference cell:

=SortListElem("rng_FormsList",$G14)
where,
rng_FormsList = OFFSET('Unique Lists'!$V$11,0,0,
COUNTIF('Unique Lists'!$V:$V,""""),1)
and say $G14 = 4

Note: To assure that I do not ask for an element larger
than the length of the range I have an if statement check
for validity.

Therefore, the formula in cell W14 is:
=IF($G14$N$9,"",
SortListElem("rng_FormsList",$G14))

Now, I copy this formula to cells W15:W25.

RESULT: Some cells have the correct value and some cells
result in #VALUE!.

During debugging, I get the same string being passed,
st_List, and the proper desired element number, m.
(AOK so far)
But, after assigning the range to an array and finding the
Ubound of the array, n, I find that the cells which have a
correct result have a value for n but the cells which
contain the #VALUE! result did not have a value for n.

Note: If I double click to edit a working result, do
nothing, and hit a return; it becomes a non-working
result. (????)

Any Ideas???
Better ways?
I can work around this by changing/adding other VBA code,
but I want to understand the error of my ways.

I have added a Application.Volatile statement, fully
defined the range (wB.wS.Range), added quotes to the
parameter string, taken them away, and many other tries.

Thanks for your help in advance,

Joe Adams



Private Function SortListElem(st_List As String, m As Long)
If m = 0 Then Exit Function
Dim varSItems() As Variant
Dim varSItems2() As Variant
Dim k As Long
Dim n As Long
''' Debug.Print st_List, m
varSItems =
Worksheets("Unique Lists").Range(st_List).Value
''' "rng_FormsList"
n = UBound(varSItems, 1)
''' Debug.Print n
ReDim Preserve varSItems(1 To n, 1 To 1)
ReDim varSItems2(1 To n)
For k = 1 To n
varSItems2(k) = varSItems(k, 1)
Next k
Call dhQuickSort(varArray:=varSItems2)
SortListElem = varSItems2(m)
End Function