Pseudo-code

iainsteel

New Member
Joined
Nov 29, 2007
Messages
31
I was hoping one of you may help me with this question:

If the median price of an apple is defined as the closest price to the price of a banana. Meaning:
Banana price is 2.2
Range of apples prices:
1
2
3
4
5
6
7

Hence in this case the answer is 2
How I write a function that would return the median apple from that list? And also how do I define the variable define the variable type you would use to accomplish this task.
I am hoping that someone can write just pseudo-code for this function.
Thanks in advance for your reply!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try this:-
Code:
Function Apple(Rng As Range, Price As Long) As Long
Dim Dn As Range
For Each Dn In rng
If Round(Dn) = Round(Price) Then
    Apple = Dn
    Exit For
End If
Next Dn
End Function
Regards Mick
 
Upvote 0
Try this:-
Code:
Function Apple(Rng As Range, Price As Long) As Long
Dim Dn As Range
For Each Dn In rng
If Round(Dn) = Round(Price) Then
    Apple = Dn
    Exit For
End If
Next Dn
End Function
Regards Mick

Maybe you should declare the variables as Double, not as Long.
 
Upvote 0
Thank you all for your response. MickG Thank you for your post!! That in particular was what I was looking for.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top