please simplify, if possible

M

Matt S

All,

I'm sort of a neat freak. In the following code, I am simply trying to take
the average of three arrays arrTop, arrMiddle, and arrBottom. They have
differing lengths, but always 11 columns. Is there a simpler way to do this?
Something that looks a little cleaner?

THANKS SO MUCH!
Matt


'Take average of data
ReDim arrAvTop(1 To 11)
ReDim arrAvMiddle(1 To 11)
ReDim arrAvBottom(1 To 11)

Sum1 = 0
Sum2 = 0
Sum3 = 0
Sum4 = 0
Sum5 = 0
Sum6 = 0
Sum7 = 0
Sum8 = 0
Sum9 = 0
Sum10 = 0
Sum11 = 0


For j = 1 To 11

For k = 1 To UBound(arrTop)
Sum1 = Sum1 + arrTop(j, k)
Sum2 = Sum2 + arrTop(j, k)
Sum3 = Sum3 + arrTop(j, k)
Sum4 = Sum4 + arrTop(j, k)
Sum5 = Sum5 + arrTop(j, k)
Sum6 = Sum6 + arrTop(j, k)
Sum7 = Sum7 + arrTop(j, k)
Sum8 = Sum8 + arrTop(j, k)
Sum9 = Sum9 + arrTop(j, k)
Sum10 = Sum10 + arrTop(j, k)
Sum11 = Sum11 + arrTop(j, k)

arrAvTop(1) = Sum1 / k
arrAvTop(2) = Sum2 / k
arrAvTop(3) = Sum3 / k
arrAvTop(4) = Sum4 / k
arrAvTop(5) = Sum5 / k
arrAvTop(6) = Sum6 / k
arrAvTop(7) = Sum7 / k
arrAvTop(8) = Sum8 / k
arrAvTop(9) = Sum9 / k
arrAvTop(10) = Sum10 / k
arrAvTop(11) = Sum11 / k
Next k

Sum1 = 0
Sum2 = 0
Sum3 = 0
Sum4 = 0
Sum5 = 0
Sum6 = 0
Sum7 = 0
Sum8 = 0
Sum9 = 0
Sum10 = 0
Sum11 = 0

For k = 1 To UBound(arrMiddle)
Sum1 = Sum1 + arrMiddle(j, k)
Sum2 = Sum2 + arrMiddle(j, k)
Sum3 = Sum3 + arrMiddle(j, k)
Sum4 = Sum4 + arrMiddle(j, k)
Sum5 = Sum5 + arrMiddle(j, k)
Sum6 = Sum6 + arrMiddle(j, k)
Sum7 = Sum7 + arrMiddle(j, k)
Sum8 = Sum8 + arrMiddle(j, k)
Sum9 = Sum9 + arrMiddle(j, k)
Sum10 = Sum10 + arrMiddle(j, k)
Sum11 = Sum11 + arrMiddle(j, k)

arrAvMiddle(1) = Sum1 / k
arrAvMiddle(2) = Sum2 / k
arrAvMiddle(3) = Sum3 / k
arrAvMiddle(4) = Sum4 / k
arrAvMiddle(5) = Sum5 / k
arrAvMiddle(6) = Sum6 / k
arrAvMiddle(7) = Sum7 / k
arrAvMiddle(8) = Sum8 / k
arrAvMiddle(9) = Sum9 / k
arrAvMiddle(10) = Sum10 / k
arrAvMiddle(11) = Sum11 / k
Next k

Sum1 = 0
Sum2 = 0
Sum3 = 0
Sum4 = 0
Sum5 = 0
Sum6 = 0
Sum7 = 0
Sum8 = 0
Sum9 = 0
Sum10 = 0
Sum11 = 0

For k = 1 To UBound(arrTop)
Sum1 = Sum1 + arrTop(j, k)
Sum2 = Sum2 + arrTop(j, k)
Sum3 = Sum3 + arrTop(j, k)
Sum4 = Sum4 + arrTop(j, k)
Sum5 = Sum5 + arrTop(j, k)
Sum6 = Sum6 + arrTop(j, k)
Sum7 = Sum7 + arrTop(j, k)
Sum8 = Sum8 + arrTop(j, k)
Sum9 = Sum9 + arrTop(j, k)
Sum10 = Sum10 + arrTop(j, k)
Sum11 = Sum11 + arrTop(j, k)

arrAvTop(1) = Sum1 / k
arrAvTop(2) = Sum2 / k
arrAvTop(3) = Sum3 / k
arrAvTop(4) = Sum4 / k
arrAvTop(5) = Sum5 / k
arrAvTop(6) = Sum6 / k
arrAvTop(7) = Sum7 / k
arrAvTop(8) = Sum8 / k
arrAvTop(9) = Sum9 / k
arrAvTop(10) = Sum10 / k
arrAvTop(11) = Sum11 / k
Next k

Next j
 
M

Matt S

Follow-up question for you...

Since I have arrTop as a two-dimensional array... how do I find the upper
bound of the second dimension? For example, arrTop(j,k)... how do I find
what the max k is? UBound(arrTop) is giving me the max j value.

Thanks,
Matt
 
M

Matt S

ok, I'm gonna stop posting... just found it using the search...

UBound(arrTop, 2)

THANKS EVERYONE! Sorry for wasting your time.
 
J

JLGWhiz

There is a lot of stuff in those help files. I keep finding new things all
the time and I have been using them for several years.
 
D

Dana DeLouis

They have differing lengths, but always 11 columns.
For j = 1 To 11
For k = 1 To UBound(arrTop)
Sum1 = Sum1 + arrTop(j, k)

Hi. Just a side note. By convention, it looks like you have 11 Rows,
and an unknown number of Columns.

Not sure if this helps, so I'll just throw it out.
This takes the Average of the first column, and the first row.
Maybe there's some ideas here you can use.

Sub Demo()
Dim m, x
m = [A1:D5]
With WorksheetFunction
'Average of first Column
x = .Average(.Index(m, 0, 1))
'Average of first Row
x = .Average(.Index(m, 1, 0))
End With
End Sub

= = =
HTH
Dana DeLouis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top