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 am trying to calculate an average of numbers in an array for a
series of individuals. The quantity of numbers may range from none to more
than 20 and I want to average no less than 2 numbers nor more than 8 numbers.
I am using Select Case as I thought that a nested If..Then.. Else would be
cumbersome. The calculation for 8 numbers works (with the exception of a
rounding issue which will be explained further down), but when I want to
calculate averages for any fewer than 8 numbers it returns erroneous values.
It seems as if it is holding onto additional numbers so the averages are much
higher than they should be.

Following is the basic code:

Dim avg As Single

' calculate average if =2, <=8 valid numbers
avg = 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

and so forth to End Select.

holdnumbers(0-7) is also cleared (set to 0) after every calculation as so:

' reset score array
holdnumbers(0) = 0
holdnumbers(1) = 0
holdnumbers(2) = 0

Also, in calculating the average the result is returned rounded to one
decimal place, though I have the worksheet set to two decimal places. The
calculated number is set as Single. For example, a worksheet average
calculated as 87.69 is returned by the code as 87.50.

Thanks in advance for any advice you can provide.
 
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
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
range.calculation with UDF not working when calculation is set to automatic Brian Murphy Excel Programming 5 October 14th 03 07:02 PM


All times are GMT +1. The time now is 11:49 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"