LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default VBA Calculation with Variables

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Calculation with Variables Hawk Excel Programming 1 March 25th 10 11:39 PM
Not at all clear on use of variables and/or object variables JMay-Rke Excel Discussion (Misc queries) 11 July 4th 08 06:36 PM
Storing variables in a macro and using those variables to performcalculations. [email protected] Excel Programming 3 December 10th 07 04:13 PM
Automating Calculation of Lagged Cross Correlations between Variables [email protected] Excel Programming 1 September 3rd 07 10:51 PM
Using Two Variables in Excel Calculation VBA Rita Excel Programming 1 September 26th 06 07:49 PM


All times are GMT +1. The time now is 08:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"