Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I am using Office 2003 on Windows XP.
In code, I need to work out which variable contains the middle value, in this example, it would be 85. I would guess this needs to be done by comparing <= and = and sometimes there could be a two or three way tie. In a tie, it doesn't matter which one is in the middle...but the code needs to identify which variable has been selected. So in the example, I would need to know that dVar1 is in the middle. Dim dVar1 as Double Dim dVar2 as Double Dim dVar3 as Double dVar1 = Abs(-85) dVar2 = Abs(95) dVar3 = Abs(25) If dVar1 = dVar2 then ... (what next? I can't crack it!) Do I have to try every combination to work this out? I can't seem to work out the code to ensure that it always works. Your help is appreciated. |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I would advise you not to develop a specialized solution that only works for
the case of 3 items since you'll need to re-solve the problem when your boss decides s/he wants to see the middle (wo)man of 5 items. The general procedure is to sort your list and take the middle item from the list. A simple explanation of bubble sort is: 1. Place your data in an array if N items 2. Create an outer loop that loops from N-1 to 1 3. Create in inner loop that loops from 0 to the value of the outer loop variable (J). 4. In the inner loop compare item(J) to item (J+1) and swap if they are out of order. 5. When the outer loop finishes the list is sorted. You can optimize it by keeping track of whether there were any swaps in an iteration of the inner loop and the first time there isn't one, your list is sorted. There are quicker sorts but this is most suitable for a problem like yours. I may have made some (hopefully) minor mistakes by regurgitating this from memory but it's a good mental exercise to verify the logic. Luke "XP" wrote in message ... I am using Office 2003 on Windows XP. In code, I need to work out which variable contains the middle value, in this example, it would be 85. I would guess this needs to be done by comparing <= and = and sometimes there could be a two or three way tie. In a tie, it doesn't matter which one is in the middle...but the code needs to identify which variable has been selected. So in the example, I would need to know that dVar1 is in the middle. Dim dVar1 as Double Dim dVar2 as Double Dim dVar3 as Double dVar1 = Abs(-85) dVar2 = Abs(95) dVar3 = Abs(25) If dVar1 = dVar2 then ... (what next? I can't crack it!) Do I have to try every combination to work this out? I can't seem to work out the code to ensure that it always works. Your help is appreciated. |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sub MiddleValue()
Dim dVar1 As Double Dim dVar2 As Double Dim dVar3 As Double Dim midval As Double Dim s As String dVar1 = Abs(-85) dVar2 = Abs(95) dVar3 = Abs(25) midval = Application.Median(dVar1, dVar2, dVar3) If dVar1 = midval Then s = "dVar1" If dVar2 = midval Then s = "dVar2" If dVar3 = midval Then s = "dVar3" Debug.Print s, midval End Sub -- Regards, Tom Ogilvy "XP" wrote: I am using Office 2003 on Windows XP. In code, I need to work out which variable contains the middle value, in this example, it would be 85. I would guess this needs to be done by comparing <= and = and sometimes there could be a two or three way tie. In a tie, it doesn't matter which one is in the middle...but the code needs to identify which variable has been selected. So in the example, I would need to know that dVar1 is in the middle. Dim dVar1 as Double Dim dVar2 as Double Dim dVar3 as Double dVar1 = Abs(-85) dVar2 = Abs(95) dVar3 = Abs(25) If dVar1 = dVar2 then ... (what next? I can't crack it!) Do I have to try every combination to work this out? I can't seem to work out the code to ensure that it always works. Your help is appreciated. |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
If the there are an odd number of variables, the Median function eliminates
all that for you. If it isn't an odd number, what would be the definition of the middle value? <g -- Regards, Tom Ogilvy "Luke Alcatel" wrote: I would advise you not to develop a specialized solution that only works for the case of 3 items since you'll need to re-solve the problem when your boss decides s/he wants to see the middle (wo)man of 5 items. The general procedure is to sort your list and take the middle item from the list. A simple explanation of bubble sort is: 1. Place your data in an array if N items 2. Create an outer loop that loops from N-1 to 1 3. Create in inner loop that loops from 0 to the value of the outer loop variable (J). 4. In the inner loop compare item(J) to item (J+1) and swap if they are out of order. 5. When the outer loop finishes the list is sorted. You can optimize it by keeping track of whether there were any swaps in an iteration of the inner loop and the first time there isn't one, your list is sorted. There are quicker sorts but this is most suitable for a problem like yours. I may have made some (hopefully) minor mistakes by regurgitating this from memory but it's a good mental exercise to verify the logic. Luke "XP" wrote in message ... I am using Office 2003 on Windows XP. In code, I need to work out which variable contains the middle value, in this example, it would be 85. I would guess this needs to be done by comparing <= and = and sometimes there could be a two or three way tie. In a tie, it doesn't matter which one is in the middle...but the code needs to identify which variable has been selected. So in the example, I would need to know that dVar1 is in the middle. Dim dVar1 as Double Dim dVar2 as Double Dim dVar3 as Double dVar1 = Abs(-85) dVar2 = Abs(95) dVar3 = Abs(25) If dVar1 = dVar2 then ... (what next? I can't crack it!) Do I have to try every combination to work this out? I can't seem to work out the code to ensure that it always works. Your help is appreciated. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Separate first name, middle name and last name | Excel Worksheet Functions | |||
Round up-down-or middle | Excel Discussion (Misc queries) | |||
how to get the middle name | Excel Worksheet Functions | |||
how to get the middle name | Excel Worksheet Functions | |||
Remove middle initial from "first name middle initial" | Excel Discussion (Misc queries) |