out of range error with Application.Average(Array(), Array())

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
I am receiving an error on the line below:

Code:
M = Application.Average(Arr(2, 8), Arr(N, 8))

Arr() is an array variable, N is an integer, Arr() is currently (1 to 2978, 1 to 9)

How can I use the average function to find the average of the values in the 8th column? Or is there another way I can find the average of a set of values in an array?

Thanks!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Code:
M = (Arr(2, 8) + Arr(N, 8)) / 2#
 
Upvote 0
I think my syntax is in error. I intended to average all the values between position 2 and N in the array. N is equal to 2878 or something. How can I average all 2000 + values?
 
Upvote 0
You'd need to write a loop that sums the values of interest and then divide by the count.
 
Last edited:
Upvote 0
If this is data read in from a worksheet, and the first row is a header (i.e., text), you could do this:

Code:
Sub SBF()
  Dim Arr           As Variant
  Dim dAvg          As Double

  Arr = Range("A1:H2978").Value2
  With WorksheetFunction
    dAvg = .Average(.Index(Arr, 0, 8))
  End With
End Sub

But in practice, I'd write a loop.
 
Upvote 0
I worked it out long hand and got a value that seems to work this way. I'll probably stick with the With statement structure. Thanks shg
 
Upvote 0
If this is data read in from a worksheet, and the first row is a header (i.e., text), you could do this:

Code:
Sub SBF()
  Dim Arr           As Variant
  Dim dAvg          As Double

  Arr = Range("A1:H2978").Value2
  With WorksheetFunction
    dAvg = .Average(.Index(Arr, 0, 8))
  End With
End Sub

But in practice, I'd write a loop.
The value generated by the above code does not seem to return the value you get by applying the AVERAGE function to the entire row. Any idea why... and why did you choose the array route when you could have just applied the Worksheet.Average function to the range directly?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top