Hi All!
Am I just being dense/stupid or does Find not work when in a function?
The background is that I need to be able to calculate the median value of a cells depending on a criteria (well 2 actually!) The ideal function would be DMEDIAN but it doesn't exist in xl97!!
So the following is the first almost working iteration of my own UDF for the purpose.
I need to find the median value in a range of data where (in this case) the first critera is "F" and the second criteria is 1.
on a worksheet
=DynaMedian(C2:C7,A2:A7,"F",B2:B7,1)
where c2:c7 hold the data to clac the median on
a2:a7 hold the first criteria (A-F)
b2:b7 hold the second criteria(1-5)
Doesn't work - found always evaluates to Nothing!
However if the above function is called like so
Works just fine!!
Any ideas, suggestions, bits I've missed, alternatives REALLY appreciated!
Oh, BTW, looping the cells isn't a realistic option as the real data set will be 25k+ rows and the function will be called at least 30 times!
;-)
Am I just being dense/stupid or does Find not work when in a function?
The background is that I need to be able to calculate the median value of a cells depending on a criteria (well 2 actually!) The ideal function would be DMEDIAN but it doesn't exist in xl97!!
So the following is the first almost working iteration of my own UDF for the purpose.
I need to find the median value in a range of data where (in this case) the first critera is "F" and the second criteria is 1.
on a worksheet
=DynaMedian(C2:C7,A2:A7,"F",B2:B7,1)
where c2:c7 hold the data to clac the median on
a2:a7 hold the first criteria (A-F)
b2:b7 hold the second criteria(1-5)
Code:
Function DynaMedian(EvalRng As Range, CritRng1 As Range, Crit1 As Variant, CritRng2 As Range, Crit2 As Variant) As Double
Dim myArr() As Variant
Dim Count As Integer
Dim found As Range
Dim firstaddress As String
With CritRng1.Cells
Set found = .Find(Crit1, LookIn:=xlValues)
If Not found Is Nothing Then
firstaddress = found.Address
Do
If Cells(found.Row, CritRng2.Column) = Crit2 Then
ReDim Preserve myArr(Count)
myArr(Count) = Cells(found.Row, EvalRng.Column).Value
Count = Count + 1
End If
Set found = .FindNext(found)
Loop While Not found Is Nothing And found.Address <> firstaddress
End If
End With
DynaMedian = WorksheetFunction.Median(myArr)
End Function
Doesn't work - found always evaluates to Nothing!
However if the above function is called like so
Code:
Sub a()
Debug.Print DynaMedian(Range("C2:c7"), Range("A2:A7"), "F", Range("B2:B7"), 1)
End Sub
Works just fine!!
Any ideas, suggestions, bits I've missed, alternatives REALLY appreciated!
Oh, BTW, looping the cells isn't a realistic option as the real data set will be 25k+ rows and the function will be called at least 30 times!
;-)
If a man says something and there are no women there to hear him, is he still wrong? ![[ponder] [ponder] [ponder]](/data/assets/smilies/ponder.gif)
How do I get the best answers?
![[ponder] [ponder] [ponder]](/data/assets/smilies/ponder.gif)
How do I get the best answers?