×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Getting nth highest value in VBA Access array

Getting nth highest value in VBA Access array

Getting nth highest value in VBA Access array

(OP)
Hello All,

I am really confused on how to get the second highest value in a Access VBA array. I can get the highest value with the below code from the Microsoft website, but I can't find how to get the second and third highest values in different functions.


Function Maximum(ParamArray FieldArray() As Variant)
' Declare the two local variables.
Dim I As Integer
Dim currentVal As Variant

' Set the variable currentVal equal to the array of values.
currentVal = FieldArray(0)

' Cycle through each value from the row to find the largest.

For I = 0 To UBound(FieldArray)
If FieldArray(I) > currentVal Then
currentVal = FieldArray(I)
End If
Next I

' Return the maximum value found.
Maximum = currentVal

End Function

Thank you in advance.
kaprmg

RE: Getting nth highest value in VBA Access array

Hi there!

how about using a temp array and TWO sorts?

CODE

Dim tmpArray
For I = 0 To UBound(FieldArray)
If FieldArray(I) > currentVal Then
currentVal = FieldArray(I)
End If
Next

tmpArray=Filter(FieldArray, currentVal, False, vbTextCompare)
'Rem: this will fill the tmpArray with all your array values EXCEPT the highest one.

secondHighest= tmpArray(0)
For I = 0 To UBound(tmpArray)
If tmpArray(I) > secondHighest Then
secondHighest= tmpArray(I)
End If
Next

Would that do the trick?
smile

"We had to turn off that service to comply with the CDA Bill."
- The Bastard Operator From Hell

RE: Getting nth highest value in VBA Access array

Function Max2nd(ParamArray FieldArray() As Variant)
' Declare the local variables.
Dim I As Integer
Dim currentVal As Variant
Dim maxVal As Variant
maxVal = Maximum(FieldArray)
' Set the variable currentVal equal to the array of values.
For I = 0 To UBound(FieldArray)
  If FieldArray(I) <> maxVal Then
    currentVal = FieldArray(I)
    Exit For
   End If
Next
' Cycle through each value from the row to find the largest.
For I = 0 To UBound(FieldArray)
  If FieldArray(I) > currentVal And FieldArray(I) < maxVal Then
    currentVal = FieldArray(I)
  End If
Next
' Return the value found.
Max2nd = currentVal
End Function

Hope This Helps, PH.
FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

RE: Getting nth highest value in VBA Access array

(OP)
Thanks MakeItSo.  The temp array worked best.  I really appreciate the help.  It is really nice to have other professionals to turn to.  thumbsup2

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close