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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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)))


.71.84.79.47
.81.86.80.85
.78.89.76.60
.79.90.75.92
.76.85.71.73
.69.83.70.90
.70.84.75.68
.75.86.75.74

<tbody>
</tbody>
 
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
Awesome

I have another range below this one, adjusted the formula, Works Perfect.

Thank You!!
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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