0

Hey guys I am trying to find the Max number from 3 given numbers in VBA, but it's not giving me what I won't. I know there are many function to get me there, like pre-built ones. But can u tell me exactly why this one won't function. It's for my school, no idea what to do. Doesn't seem to go past B parameter.

Function Max(A As Integer, B As Integer, C As Integer) As Integer
Max = A
If Max < B Or B > C Then
Max = B
ElseIf A < C Or B < C Then
Max = C
Else
Max = 0
End If
End Function

1 Answer 1

0

You can get the maximum of three values by just checking each value in turn.

  • Assume the first is the highest to start with.
  • Then, if the current highest is less than the second, replace it with the second.
  • Then do the same with the third.

The code for that would go something like this:

Function MaxOf3(num1 As Integer, num2 As Integer, num3 As Integer) As Integer
    MaxOf3 = num1
    If MaxOf3 < num2 Then MaxOf3 = num2
    If MaxOf3 < num3 Then MaxOf3 = num3
End Function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.