The code works great except if the first field is a zero or all the fields in the row contain zeros (see pic).
Many thanks! - Function Maximum(ParamArray FieldArray() As Variant)
-
' Declare the two local variables.
-
Dim I As Integer
-
Dim currentVal As Variant
-
Dim secondHighest 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.
-
-
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)
-
'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
-
-
' Return the maximum value found.
-
Maximum = secondHighest
-
-
' Expr1: Maximum ([nPP1CSF],[nPP2CSF],[nPP3CSF],[nPP4CSF],[nPP5CSF],[nPP6CSF],[nPP7CSF],[nPP8CSF],[nPP9CSF],[nPP0CSF])
-
-
End Function
30 3379
Liam,
Welcome to Bytes! There are a couple issues keeping you from the answer you want. First, anytime all the numbers are the same (not just zeroes), the code won't be able to handle it because you're filtering out all the variables. You can solve this by counting how many times the numbers are the same--if they are always the same, just force insert what you want into the field (since there is no second-highest number, I'm not sure what this would be). Also, you'll run into some interesting problems if you leave the values as variables. I changed them to integers in the code below. -
Function Maximum(ParamArray FieldArray() As Variant)
-
' Declare the two local variables.
-
Dim I As Integer
-
Dim currentVal As Integer
-
Dim secondHighest As Integer
-
Dim intCount As Integer
-
-
' 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)
-
ElseIf FieldArray(I) = currentVal Then
-
intCount = intCount + 1
-
End If
-
Next
-
-
If intCount - 1 = UBound(FieldArray) Then
-
' Return the starting value since they were all the same
-
Maximum = FieldArray(0) 'MODIFY THIS TO WHATEVER YOU WANT WHEN THEY ARE ALL THE SAME
-
Else
-
Dim tmpArray As Variant
-
tmpArray = Filter(FieldArray, currentVal, False, vbTextCompare)
-
' 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
-
-
' Return the maximum value found.
-
Maximum = secondHighest
-
End If
-
-
' Expr1: Maximum ([nPP1CSF],[nPP2CSF],[nPP3CSF],[nPP4CSF],[nPP5CSF],[nPP6CSF],[nPP7CSF],[nPP8CSF],[nPP9CSF],[nPP0CSF])
-
-
End Function
-
liamthequietman,
If you will indulge me the opportunity to modify gnawoncents's excellent suggestion.... - Function Maximum(ParamArray FieldArray() As Variant)
-
On Error GoTo EH
-
' Declare the two local variables.
-
Dim I As Integer
-
Dim currentVal As Integer
-
Dim secondHighest As Integer
-
Dim intCount As Integer
-
-
' 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 intCount = intCount + 1
-
If FieldArray(I) > currentVal Then
-
secondHighest = currentVal
-
currentVal = FieldArray(I)
-
Else
-
If FieldArray(I) > secondHighest _
-
And FieldArray(I) < currentVal Then
-
secondHighest = FieldArray(I)
-
End If
-
End If
-
Next
-
-
If intCount - 1 = UBound(FieldArray) Then
-
' Return the starting value since they were all the same
-
Maximum = FieldArray(0) 'MODIFY THIS TO WHATEVER YOU WANT WHEN THEY ARE ALL THE SAME
-
Else
-
' Return the maximum value found.
-
Maximum = secondHighest
-
End If
-
-
Exit Function
-
EH:
-
MsgBox "There was an error finding the second highest value! " & _
-
"Please contact your Database Administrator.", vbCritical, "WARNING!"
-
Exit Function
-
End Function
If you look closely above, I've eliminated the need to go through your array twice. Assuming your beginning highest value is in currentVal (which will begin at 0), then if there is any value greater than that, the higher value becomes currentVal . But, before we do that, we asign the value of currentVal to secondHighest (which initially will also be 0). Then if any future values are greater than currentVal , then secondHighest takes on the value of the previous highest value. There is an Else added to the code to handle cases in which the new value is less than currentVal but greater than secondHighest .
This should give you a more streamlined code (and faster, at that).
Hope this hepps!
twinnyfo,
Very nice--much more elegant. *initiates slow clap*
gnawoncents,
Aww shucks.... I just stole what you posted!
:-P
BRILLIANT!
What an excellent collaboration between the two of you!
I hope others can benefit from this excellent code.
I'm very grateful!
Glad the two of us could hepp!
I've made changes to the code for "Minimum" values "Second Lowest" The values are Single and contain -1
It works perfect except if a -1 appears in any of the columns.
Greatly appreciated!
(see pic) - Function Minimum(ParamArray FieldArray() As Variant)
-
On Error GoTo EH
-
' Declare the two local variables.
-
Dim I As Integer
-
Dim currentVal As Single
-
Dim secondLowest As Single
-
Dim intCount As Integer
-
-
' Set the variable currentVal equal to the array of values.
-
currentVal = FieldArray(0)
-
-
' Cycle through each value from the row to find the lowest.
-
For I = 0 To UBound(FieldArray)
-
If FieldArray(I) = currentVal Then intCount = intCount + 1
-
If FieldArray(I) < currentVal Then
-
secondLowest = currentVal
-
currentVal = FieldArray(I)
-
Else
-
If FieldArray(I) < secondLowest _
-
And FieldArray(I) > currentVal Then
-
secondLowest = FieldArray(I)
-
End If
-
End If
-
Next
-
-
If intCount - 1 = UBound(FieldArray) Then
-
' Return the starting value since they were all the same
-
Minimum = FieldArray(0) 'Modify this to whatever you want when they are all the same
-
Else
-
' Return the Minimum value found.
-
Minimum = secondLowest
-
End If
-
-
Exit Function
-
EH:
-
MsgBox "There was an error finding the second Lowest value! " & _
-
"Please contact your Database Administrator.", vbCritical, "WARNING!"
-
Exit Function
-
-
' Expr1: Minimum([nPP1SHF],[nPP2SHF],[nPP3SHF],[nPP4SHF],[nPP5SHF],[nPP6SHF],[nPP7SHF],[nPP8SHF],[nPP9SHF],[nPP0SHF])
-
-
End Function
All you need to do is reverse you ">" and "<" operators.
I edited my post. Please read again I almost have the solution!
Thanks!
liam - for future posts, please use Code Tags when posting your code. The moderators are able to edit your content, but we shouldn't have to do that every time.
What happens when there is a -1 in any of the values. It should work with positive or negative numbers.
I'm a novice and will now Google "Code Tags" to understand what they are.
My apologies!
My changes to the code work perfect except if a -1 appears in any column.
see pic in post I edited
Code tags are what you place your code in so that they are properly formatted on this forum. There is a button in the text entry display "[CODE/] which will insert code tags around any of your selected text.
Your pic does not tell me what the code is doing wrong. It looks to me to be doing exactly what the code is telling it to do. What do you want the result to be when it is all -1? Are these by chance text values?
If there is a column(s) with a -1 it is returning the lowest number, not the second lowest.
Columns without -1 are returning the correct second lowest number
Columns with all -1 returns -1 which is perfect
Have you stepped through the code while it is running to see how the evaluations of the values are working? Particularly, when there is a -1 value and the code gets here: - If FieldArray(I) < currentVal Then
-
secondLowest = currentVal
-
currentVal = FieldArray(I)
-
Else
-
If FieldArray(I) < secondLowest _
-
And FieldArray(I) > currentVal Then
-
secondLowest = FieldArray(I)
-
End If
-
End If
How are the evaluations responding?
Not sure if it has anything to do with it, but you may need to declare your array as a Double? - Function Minimum(ParamArray FieldArray() As Double)
Being a complete VBA novice, I'm not familiar on how to step through the code. I placed my cursor in the code and pressed F8 and received a ping which I associate with an error.
Declaring the array as Double resulted in a error
No worries, we've all been novices at one point.
Before you run your code, click your mouse to the left of the first line of your function in the VBA editor. It should leave a dark brown circle in the left margin. Run your code and the code should stop at that point (called a break point). Then, hit F8 repeatedly to watch the code work through itself. Any time the code is paused, you can hover your mouse over any of the variables and you will be able to see their current values.
Hope that hepps!
Thanks for not giving up on this project!
My step through attempt did not work, a Macro box appears (see pic).
I found another issue with the results. In review:
CORRECT
Columns without -1 are returning the correct second lowest number
Columns with all -1 returns -1 which is perfect
INCORRECT
If there is a column(s) with a -1 it is returning the lowest number, not the second lowest.
If there are columns with identical numbers it's returning zero
By "running your code", I mean you have to execute it the way you would normally execute it. Then, when the code hits your break point, then you can step through it using F8. Hope that hepps.
Or, maybe try using the values -2 and -1.2 to see if that will have any effect on the logic the code returns. At this point, I am grasping at straws, because based upon what I am seeing int he code, it "should" work, but it just doesn't. I must be missing something obvious, which would not be the first time something like that happened.
Was able to do some testing and proofing. Note lines 23-31. We forgot about the situation when the first number is the lowest. We never checked to see if the second was still the lowest value. This should solve it. - Public Function Minimum(FieldArray() As Variant) As Single
-
On Error GoTo EH
-
' Declare the two local variables.
-
Dim I As Integer
-
Dim currentVal As Single
-
Dim secondLowest As Single
-
Dim intCount As Integer
-
-
' Set the variable currentVal equal to the array of values.
-
currentVal = FieldArray(0)
-
secondLowest = FieldArray(0)
-
-
' Cycle through each value from the row to find the lowest.
-
For I = 0 To UBound(FieldArray)
-
If FieldArray(I) = currentVal Then intCount = intCount + 1
-
If FieldArray(I) < currentVal Then
-
secondLowest = currentVal
-
currentVal = FieldArray(I)
-
ElseIf secondLowest = currentVal _
-
And FieldArray(I) > secondLowest Then
-
secondLowest = FieldArray(I)
-
Else
-
If secondLowest = currentVal _
-
And FieldArray(I) > secondLowest Then
-
secondLowest = FieldArray(I)
-
Else
-
If FieldArray(I) < secondLowest _
-
And FieldArray(I) > currentVal Then
-
secondLowest = FieldArray(I)
-
End If
-
End If
-
End If
-
Next
-
-
If intCount - 1 = UBound(FieldArray) Then
-
' Return the starting value since they were all the same
-
Minimum = FieldArray(0) 'Modify this to whatever you want when they are all the same
-
Else
-
' Return the Minimum value found.
-
Minimum = secondLowest
-
End If
-
-
Exit Function
-
EH:
-
MsgBox "There was an error finding the second Lowest value! " & _
-
"Please contact your Database Administrator.", vbCritical, "WARNING!"
-
Exit Function
-
End Function
Can I attach a test version of my database?
Check my last post and see if that code works. It ran fine for me. I knew I was overlooking something obvious. It wasn't until I was able to step through the code that I found it.
I applaud you for your tenacity! The expression I enter into the query has worked in all your codes except now. Error wrong number of arguments:
Expr1: Minimum([nPP1SHF],[nPP2SHF],[nPP3SHF],[nPP4SHF],[nPP5SHF],[nPP6SHF],[nPP7SHF],[nPP8SHF],[nPP9SHF],[nPP0SHF])
I changed Line 1 from: - Public Function Minimum(ParamArray FieldArray() As Variant) As Single
to: - Public Function Minimum(FieldArray() As Variant) As Single
Not sure if that makes a difference. I couldn't get it to work any other way.
I was using my code from within VBA, you are calling it as an expression in a Query, which is causing the disconnect.
I found a code which works perfect EXCEPT ...
If there is a column(s) with a -1 it is returning the lowest number, not the second lowest.
Otherwise all the result are correct. - Function SecondMinimum(ParamArray FieldArray() As Variant) As Variant
-
Dim I As Integer
-
Dim LowestVal As Variant
-
Dim secondVal As Variant
-
-
' Default values
-
LowestVal = Null
-
secondVal = Null
-
-
' Populate the LowestVal and secondVal temporary variables
-
For I = 0 To UBound(FieldArray)
-
If IsNull(FieldArray(I)) = False Then
-
If IsNull(LowestVal) Then
-
LowestVal = FieldArray(I)
-
ElseIf IsNull(secondVal) Then
-
' Before populating the secondVal, make sure LowestVal will be
-
' less than or equal to secondVal.
-
If FieldArray(I) > LowestVal Then
-
secondVal = FieldArray(I)
-
Else
-
secondVal = LowestVal
-
LowestVal = FieldArray(I)
-
End If
-
Exit For
-
End If
-
End If
-
Next I
-
-
' Make sure there are at least two non-Null values in the temporary variables
-
If IsNull(LowestVal) = False And IsNull(secondVal) = False Then
-
-
' Loop through the array values, comparing them to the lowest and second-lowest
-
For I = 0 To UBound(FieldArray)
-
' Skip duplicate values.
-
If FieldArray(I) <> LowestVal Then
-
' New lowest value?
-
If FieldArray(I) < LowestVal Then
-
' New lowest value. Push the Lowest up to second, then make this
-
' array value the new lowest value.
-
secondVal = LowestVal
-
LowestVal = FieldArray(I)
-
' Skip duplicate values.
-
ElseIf FieldArray(I) <> secondVal Then
-
' New second-lowest value?
-
If FieldArray(I) < secondVal Then
-
' Replace the second-lowest value with the current array value.
-
secondVal = FieldArray(I)
-
End If
-
End If
-
End If
-
Next I
-
End If
-
-
' This function will return Null if less than 2 non-Null values were passed.
-
SecondMinimum = secondVal
-
-
' Expr1: SecondMinimum([nPP1SHF],[nPP2SHF],[nPP3SHF],[nPP4SHF],[nPP5SHF],[nPP6SHF],[nPP7SHF],[nPP8SHF],[nPP9SHF],[nPP0SHF])
-
-
End Function
-
Did you adjust your code to match what I provided? It worked perfectly for me with all values.
I just ran the code in post #23. When I place the expression into the query I receive, error wrong number of arguments:
Expr1: Minimum([nPP1SHF],[nPP2SHF],[nPP3SHF],[nPP4SHF],[nPP5SHF],[nPP6SHF],[nPP7SHF],[nPP8SHF],[nPP9SHF],[nPP0SHF])
Did you change line 1 as discussed in post #27?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: shank |
last post by:
How do you return the highest value in a recordset of maybe 100 records?
Is it necessary to run 2 recordsets?
I was hoping it was as simple as Max(), but no luck.
thanks
|
by: Jaspreet |
last post by:
I was working on some database application and had this small task of
getting the second highes marks in a class. I was able to do that using
subqueries.
Just thinking what is a good way of...
|
by: Ada |
last post by:
hello folks,
is there a way to retrieve the highest value in the ArrayList?
let say i have a value in the array: {1, 4, 15, 3, 7}
it should return a value 15 as the result.
i've looked at...
|
by: Jan |
last post by:
Hi there,
Is there a fast way to get the highest value from an array?
I've got the array strStorage(intCounter)
I tried something but it all and's to nothing
If someone good helpme, TIA
|
by: tathagata |
last post by:
I want second highest value in a table without using subquery.
please send this answer .It will be very helpfull.
|
by: =?Utf-8?B?cm9kY2hhcg==?= |
last post by:
hey all,
i have an int array and was just wondering if there is a way to get the
highest value in the array?
for instance,
int myValues = new int { 0, 1, 2 }
highest value is 2.
thanks,
|
by: faizan qazi |
last post by:
Hello
Greetings
Guide Me To Find Second Highest Number In Array
|
by: konaravikumar |
last post by:
writing a query to get the second highest value in atable by using select statement
|
by: farzadaumixer |
last post by:
hi umm
i need help in writing a program in c(or c++)
here are the details:
i want it to recieve "x" numbers of student id's and the average of the student in their last term
and i want it to...
|
by: Sergenj |
last post by:
I would like to find the highest value within an array of complex numbers.
Let assume this is the function I want to modify in order to get the highest values of array y :
//iterDelayEst.cpp...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |