Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #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...
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default VBA Calculation with Variables


I would like to see the code that puts the data into the array. I
could fix the code you posted but it is better to fix the entire code.
there are either tow ways of handling an array of scores that may have
diffferent lengths.


1) Put the scores into an array containing the exact length of rounds
of golf played using REDIM


Suppose the scores are in a spreadsheet like this

A B C D E F G
Bob 72 82 80 92 100 85
**** 82 92 85
Bob 72 90

Dim Scores() as variant
LastRow = Range("A" & rows.count).end(xlup).Row
for Rowcount = 2 to LastRow
ColCount = 2
Index = 0
Do while Cells(RowCount,ColCount) < ""
Redim Scores(0 to Index)
Scores(0) = Cells(RowCount,Colcount)

ColCount = Colcount + 1
Index = Index + 1
loop
next RowCount

Now the average would be simply
Average = WorksheetFunction.Average(Scores)


I know I've simplified the problems and didn't include using the best
scores but I was just trying to show different methods.


2) Use a multi-dimension array and add a column to the array which
contains the number of round of golf a person has played.
Then average the number of rounds of golf a person have played using
the following

Worksheet.function.Sum(Scores)/RoundsPlayed

Since you have in the array zeroes if less than 10 rounds werre
played.


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=190745

http://www.thecodecage.com/forumz

Reply
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 11:36 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"