Insert Average into ranges

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Good afternoon

Trying to insert the worksheet.Function.Average for a specific range of rows

"A2:A9" for columns 5 then "A2:A9 in column 6 and so on. Then there are columns 11 to 14 where I do not wish to insert any average and continue on with columns 15 to 19.

Thank You
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Table example below. Hopeful this will provide further explanation to provide an average (red cells) for the range above.

current code = Cells(8, 8).Value = WorksheetFunction.Average(Range(Cells(1, 8), Cells(7, 8)))


[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD].71[/TD]
[TD].84[/TD]
[TD].79[/TD]
[TD].47[/TD]
[/TR]
[TR]
[TD].81[/TD]
[TD].86[/TD]
[TD].80[/TD]
[TD].85
[/TD]
[/TR]
[TR]
[TD].78[/TD]
[TD].89[/TD]
[TD].76[/TD]
[TD].60[/TD]
[/TR]
[TR]
[TD].79[/TD]
[TD].90[/TD]
[TD].75[/TD]
[TD].92[/TD]
[/TR]
[TR]
[TD].76[/TD]
[TD].85[/TD]
[TD].71[/TD]
[TD].73[/TD]
[/TR]
[TR]
[TD].69[/TD]
[TD].83[/TD]
[TD].70[/TD]
[TD].90[/TD]
[/TR]
[TR]
[TD].70[/TD]
[TD].84[/TD]
[TD].75[/TD]
[TD].68[/TD]
[/TR]
[TR]
[TD].75[/TD]
[TD].86[/TD]
[TD].75[/TD]
[TD].74[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
My latest attempt to insert average below row 14 in columns 2 to 6

For i = 2 To 6

'Columns B to F

For rrow = 2 To 14

.Rows 2 to 14

Cells(rrow + 1, i) = WorksheetFunction.Average(Range(Cells(2, i), Cells(rrow, i)))


Next i
Next rrow
 
Upvote 0
You can do that without a loop. Try:

Code:
Range("B15:F15").FormulaR1C1 = "=AVERAGE(R[-13]C:R[-1]C)"

which puts a formula in B15:F15 that calculates the average of the rows 2-14 above. If you want the actual value instead of a formula, add this line:

Code:
    Range("B8:E8").FormulaR1C1 = "=AVERAGE(R[-7]C:R[-1]C)"
    Range("B8:E8").Value = Range("B8:E8").Value

which converts the formulas to values.
 
Upvote 0
Sure, that's the R1C1 style of referencing. You're probably familiar with the A1, or B3:C5 type of referencing. The letter is the column, the number is the row. Excel has another way to reference a cell, R stands for row, and C stands for column. So R2C3 is the same as C2 (row 2, column 3). If you leave the number off in a formula (there's no number after the C), that means the current row or column. If you put [] around the number, that's the relative row or column.

So in this formula:
Range("B15:F15").FormulaR1C1 = "=AVERAGE(R[-13]C:R[-1]C)"

R[-13]C:R[-1]C means, "Starting in the cell that this formula is placed in (B15), to get to the first cell in this formula, go up 13 rows and stay in this column, and the next cell go up 1 row and stay in this column. You can see that when you define the range that way, the exact same formula will work for all the other cells too, C8:E8. That's a handy VBA trick. :)
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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