Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hello: I'm trying to modify a program I wrote with some help a number of
years ago and find that I have fallen behind in programming thought. The program currently scans each row (representing a person) and places up to the last eight 0 values into an array which is then sorted low to high numbers. In that version the 4 lowest values were summed and an average returned . The revision is to place up to the last ten 0 values into the array and sort low to high. This is where the change takes place in that now I want to calculate an average if there are anywhere from 2 to 8 values. I started with If.. Then.. Else and it seemed to get complex so I tried Select Case but my code only works with 8 values. Below is that portion of the code: Dim golfavg as Single ' calculate average if =2, <=8 valid scores golfavg = 0 Select Case 8 - 0 Case 8 If holdnumbers(0) 0 And holdnumbers(1) 0 And holdnumbers(2) 0 And holdnumbers(3) 0 And holdnumbers(4) 0 And holdnumbers(5) 0 And holdnumbers(6) 0 And holdnumbers(7) 0 Then golfavg = (holdnumbers(0) + holdnumbers(1) + holdnumbers(2) + holdnumbers(3) + holdnumbers(4) + holdnumbers(5) + holdnumbers(6) + holdnumbers(7)) / 8 End If Case 7 If holdnumbers(0) 0 And holdnumbers(1) 0 And holdnumbers(2) 0 And holdnumbers(3) 0 And holdnumbers(4) 0 And holdnumbers(5) 0 And holdnumbers(6) 0 Then golfavg = (holdnumbers(0) + holdnumbers(1) + holdnumbers(2) + holdnumbers(3) + holdnumbers(4) + holdnumbers(5) + holdnumbers(6)) / 7 End If ....the code continues in this manner through Case 2. Case 0 - 1 golfavg = 0 End Select It seems the code is looking strictly at Case 8 and not moving on to the other Case statements if less than 8 values. This presents another problem that when there are less than 8 values it returns very large numbers as if additional numbers are being added to the array or the array is not being cleared in those cases so I'm assuming that the code is not reaching End Select even though it runs through. Range("AI" & j).Value = golfavg ' reset score array holdnumbers(0) = 0 holdnumbers(1) = 0 holdnumbers(2) = 0 holdnumbers(3) = 0 holdnumbers(4) = 0 holdnumbers(5) = 0 holdnumbers(6) = 0 holdnumbers(7) = 0 holdnumbers(8) = 0 holdnumbers(9) = 0 Next Range("B3").Select End Sub Also, the returned averages are being rounded to the nearest .50 even though I have the worksheet column set to numeric with 2 decimal places. Any help is appreciated. Thanks... |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
VBA Calculation with Variables | Excel Programming | |||
Not at all clear on use of variables and/or object variables | Excel Discussion (Misc queries) | |||
Storing variables in a macro and using those variables to performcalculations. | Excel Programming | |||
Automating Calculation of Lagged Cross Correlations between Variables | Excel Programming | |||
Using Two Variables in Excel Calculation VBA | Excel Programming |