a.riva@UCL said:
Thanks Alan.
I'll download the file straight away! And I'll try the code
Cheers!!!
Antonio.
Here is a more general function that is intended to mirror with arrays
(or ranges, for that matter) the operation of the RANK worksheet
function; watch for wordwrap. It depends on other functions in the
freely downloadable file at
http://home.pacbell.net/beban.
Constructive comments welcome.
Function ArrayRank(varValue, varArray, Optional varOrder = 0)
'This function is designed to operate on arrays as the
'worksheet RANK function operates on ranges.
Dim arrOut, numDimsV As Integer, i As Long, j As Long
Dim varArrayDupe, varValueDupe
'Return a single rank for a single variable.
If Not IsArray(varValue) Then
'Reject non-numeric input.
If Not IsNumeric(varValue) Then
Msg = "the first input parameter must be a number" & _
"or a range or array of numbers."
MsgBox Msg, 16
Exit Function
End If
If Application.IsNA(Application.Match(varValue, varArray, 0)) Then
ArrayRank = "#N/A"
ElseIf varOrder = 0 Then
ArrayRank = ArrayCountIf(varArray, varValue, ">") + 1
Else
ArrayRank = ArrayCountIf(varArray, varValue, "<") + 1
End If
Else
'Convert ranges, if any, to arrays
varArray = varArray
varValue = varValue
'To insure numeric values, convert input to Long() type arrays
ReDim varArrayDupe(1) As Long
ReDim varValueDupe(1) As Long
'Assign varValue to a Long() type array
xV = Assign(varValue, varValueDupe)
'If varValue contains values not convertible to Longs, the
'assignment will fail, so
If xV = False Then Exit Function
'Assign varArray to a Long() type array
xA = Assign(varArray, varArrayDupe)
'If varArray contains values not convertible to Longs, the
'assignment will fail, so
If xA = False Then Exit Function
numDimsV = ArrayDimensions(varValueDupe)
Select Case numDimsV
'If the input values to be ranked are in a 1-dimensional array,
return
'a same sized 1-dimensional array of rank values.
Case 1
'Load a 1-dimensional output array.
ReDim arrOut(LBound(varValueDupe) To UBound(varValueDupe) -
LBound(varValueDupe) + 1)
For i = LBound(varValueDupe) To UBound(varValueDupe) -
LBound(varValueDupe) + 1
If varOrder = 0 Then
If IsError(ArrayMatch(varValueDupe(i),
varArrayDupe)) Then
arrOut(i) = "#N/A"
Else
arrOut(i) = ArrayCountIf(varArrayDupe,
varValueDupe(i), ">") + 1
End If
Else
If IsError(ArrayMatch(varValueDupe(i),
varArrayDupe)) Then
arrOut(i) = "#N/A"
Else
arrOut(i) = ArrayCountIf(varArrayDupe,
varValueDupe(i), "<") + 1
End If
End If
i = i + 1
Next
'If the input values to be ranked are in a 2-dimensional array,
return
'a same sized 2-dimensional array of rank values.
Case 2
'Load a 2-dimensional output array.
ReDim arrOut(LBound(varValueDupe) To UBound(varValueDupe) -
LBound(varValueDupe) + 1, _
LBound(varValueDupe, 2) To UBound(varValueDupe,
2) - LBound(varValueDupe, 2) + 1)
For i = LBound(varValue) To UBound(varValue) -
LBound(varValue) + 1
For j = LBound(varValueDupe, 2) To UBound(varValueDupe,
2) - LBound(varValueDupe, 2) + 1
If varOrder = 0 Then
If IsError(ArrayMatch(varValueDupe(i, j),
varArrayDupe)) Then
arrOut(i, j) = "#N/A"
Else
arrOut(i, j) = ArrayCountIf(varArrayDupe,
varValueDupe(i, j), ">") + 1
End If
Else
If IsError(ArrayMatch(varValueDupe(i, j),
varArrayDupe)) Then
arrOut(i, j) = "#N/A"
Else
arrOut(i, j) = ArrayCountIf(varArrayDupe,
varValueDupe(i, j), "<") + 1
End If
End If
Next
Next
End Select
ArrayRank = arrOut
End If
End Function
Alan Beban